Remove WooCommerce Script and CSS files in WordPress

Featured Video Play Icon

WooCommerce Tutorial: In simple steps, going to show you how to remove Woocommerce script and css files in WordPress from unnecessary pages. The following codes are tested by using WordPress Child Theme and working absolutely fine.

About WooCommerce: Woocommerce is a great and widely used WordPress plugin for a WordPress site to add e-commerce functionalities. Its great support, docs, and add-ons make it first choice among WordPress users for both developers and non-developers.

Why remove Woocommerce script and css files in WordPress?

As a great e-commerce plugin, woocommerce load too many JS and CSS files as soon as you activate it. Those JS and CSS files load in all posts and pages where you don’t need including woocommerce relative pages. Site loading speed also increases and visitors get disturbed which is not good for any e-commerce site owner.

To get over this problem, here I am going to give you the most effective WooCommerce JS and CSS file optimization tips without disturbing site performance, visitors and eCommerce functionalities.

Here is how to do?

Step 1: Open functions.php file (Assuming you are using WordPress child theme)

Step 2: Copy and paste this below codes

//* Dequeue scripts and styles
add_action( 'wp_enqueue_scripts', 'bn_disable_woocommerce_loading_css_js' );

function bn_disable_woocommerce_loading_css_js() {

// Check if WooCommerce plugin is active
if( function_exists( 'is_woocommerce' ) ){

// Check if it's any of WooCommerce page
if(! is_woocommerce() && ! is_cart() && ! is_checkout() ) {         

    ## Dequeue WooCommerce styles
    wp_dequeue_style('woocommerce-layout'); 
    wp_dequeue_style('woocommerce-general'); 
    wp_dequeue_style('woocommerce-smallscreen');
    wp_dequeue_style('wc-block-style');

    ## Dequeue WooCommerce scripts
    wp_dequeue_script('wc-cart-fragments');
    wp_dequeue_script('woocommerce'); 
    wp_dequeue_script('wc-add-to-cart'); 
    wp_deregister_script( 'js-cookie' );
    wp_dequeue_script( 'js-cookie' );
    wp_dequeue_script( 'vc_woocommerce-add-to-cart-js' );

}
}   
}

Step 3: Save it and you are done

Step 4: Now check your site performance speed. You will see the improvements

Step 5: Clarification:

With add_action, I call the ‘ bn_disable_woocommerce_loading_css_js ’ functions with wp_enqueue_scripts. Next in actual functions, First I checked woocommerce functions exits or not with “ is_woocommerce ”. Then dequeue the woocommerce css and js files except woocommerce, cart, checkout pages.