WC 8.5.0 – Provide ways to remove unwanted scripts

[ad_1]

I’m not sure if it’s still valid, you’d have to test, but a few years back someone happened to create a way to do so. Again, not sure if it still works, you may need to adjust to your needs. Add to your child theme’s functions.php

// This removes WooCommerce Scripts on unnecessary pages
function woocommerce_de_script() {
    if (function_exists( 'is_woocommerce' )) {
     if (!is_woocommerce() && !is_cart() && !is_checkout() && !is_account_page() ) { // if we're not on a Woocommerce page, dequeue all of these scripts
    	wp_dequeue_script('wc-add-to-cart');
    	wp_dequeue_script('jquery-blockui');
    	wp_dequeue_script('jquery-placeholder');
    	wp_dequeue_script('woocommerce');
    	wp_dequeue_script('jquery-cookie');
    	wp_dequeue_script('wc-cart-fragments');
      }
    }
}
add_action( 'wp_print_scripts', 'woocommerce_de_script', 100 );
add_action( 'wp_enqueue_scripts', 'remove_woocommerce_generator', 99 );
function remove_woocommerce_generator() {
    if (function_exists( 'is_woocommerce' )) {
	if (!is_woocommerce()) { // if we're not on a woo page, remove the generator tag
		remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
	}
    }
}

// This removes WooCommerce Styles, then add woo styles back in on woo-related pages
function child_manage_woocommerce_css(){
    if (function_exists( 'is_woocommerce' )) {
	if (!is_woocommerce()) { // this adds the styles back on woocommerce pages. If you're using a custom script, you could remove these and enter in the path to your own CSS file (if different from your basic style.css file)
		wp_dequeue_style('woocommerce-layout');
		wp_dequeue_style('woocommerce-smallscreen');
		wp_dequeue_style('woocommerce-general');
	}
    }
}
add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_css' );

Dear @serafinnyc,

Thanks, but my question is related to the new wc-block scripts, and none of the above codes.

I removed successfully, that is not a problem for me, but it gives warnings after that, which feels like the core can’t work properly wihout it.

This is related to blocks as well. 4th or 5th line down . Again debug and then adjust accordingly.

I’m not seeing any issues on my end. Could be conflicting with another app or script.

I think we’re not in the same boat.

Till WC 8.5.0 we’ve 30+ new scripts thanks to WooCommerce Blocks integration. The code what I mentioned removes them by 1 line, but it shows warning after that in the Console for payment methods like the basic bacs

Payment gateway with handle 'wc-payment-method-bacs' has been deactivated in Cart and Checkout blocks because its dependency 'wc-blocks-registry' is not registered. Read the docs about registering assets for payment methods: https://github.com/woocommerce/woocommerce-blocks/blob/060f63c04f0f34f645200b5d4da9212125c49177/docs/third-party-developers/extensibility/checkout-payment-methods/payment-method-integration.md#registering-assets

I reported this to get feedback from the plugin author level member.
It is something with the payment methods and the wc-blocks.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer