Disable WooCommerce Scripts and Styles

September 18, 2013 in Blog, Code Snippets by

Here’s how to disable all the extra scripts and style-sheets WooCommerce loads throughout your web site, and restricts them to just loading in your store area. Just add this piece of code into themes functions.php file.

add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );
/**
 * Remove WooCommerce Generator tag, styles, and scripts from all areas other than store
 * Tested and works with WooCommerce 2.0+
 */
function child_manage_woocommerce_styles() {
	remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
	if ( !is_woocommerce() && !is_page('store') && !is_shop() && !is_product_category() && !is_product() && !is_cart() && !is_checkout() ) {
		wp_dequeue_style( 'woocommerce_frontend_styles' );
		wp_dequeue_style( 'woocommerce_fancybox_styles' );
		wp_dequeue_style( 'woocommerce_chosen_styles' );
		wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
		wp_dequeue_script( 'wc_price_slider' );
		wp_dequeue_script( 'wc-single-product' );
		wp_dequeue_script( 'wc-add-to-cart' );
		wp_dequeue_script( 'wc-cart-fragments' );
		wp_dequeue_script( 'wc-checkout' );
		wp_dequeue_script( 'wc-add-to-cart-variation' );
		wp_dequeue_script( 'wc-single-product' );
		wp_dequeue_script( 'wc-cart' );
		wp_dequeue_script( 'wc-chosen' );
		wp_dequeue_script( 'woocommerce' );
		wp_dequeue_script( 'prettyPhoto' );
		wp_dequeue_script( 'prettyPhoto-init' );
		wp_dequeue_script( 'jquery-blockui' );
		wp_dequeue_script( 'jquery-placeholder' );
		wp_dequeue_script( 'fancybox' );
		wp_dequeue_script( 'jqueryui' );
}}

About Milan

The founder of Dessky, Milan has worked in all aspects of advanced web development, from building large commercialized e-commerce and social network systems to troubleshooting small wordpress blogs. His extensive skills cover virtually every area of web development. Milan works hard to implement tomorrow’s trends utilizing the cutting edge systems of today. He specializes in rich internet web application development and deployment, complex HTML5/CSS3 graphical design layouts, full blown framework-driven rich internet applications, and much more. Milan provides elegant solutions to complex problems encountered by businesses that use internet based services. Also he is the MSc of Computer Science.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...Loading...

6 responses to Disable WooCommerce Scripts and Styles

  1. Theo says:

    Hi Milan
    Sorry to bother you, but i am little confused about the logic.
    How about: if ( ! is_woocommerce() || ! is_page(‘store’) ||! is_shop() || ! is_product_category() || ! is_product() || ! is_cart() || ! is_checkout() ){ dequeue}
    regards
    theo

    • Milan says:

      Hey Theo,

      Thanks for pointing this logic error to us, previously it was written wrong and it should look like this instead:

      if (!is_woocommerce() && !is_page(‘store’) && !is_shop() && !is_product_category() && !is_product() && !is_cart() && !is_checkout()){ dequeue}

      Snippet is now updated with the correct logic.

      Cheers!

      Milan

  2. Andrea says:

    Hi,
    Woocommerce has undergone many upgrades since this article was postest and is now adding more/other script and style files which I would like to only load on my shop pages. Would you by chance know how to dequeue the other files as well?

    I looked at the source code and I would like to dequeue the following scripts and styles on all pages except cart, checkout and a page with the ID 4686(this is a regular page I have with a woocommerce shortcode on it):

    /assets/css/select2.css
    /assets/js/frontend/add-to-cart.min.js
    /assets/js/select2/select2.min.js
    /assets/js/jquery-blockui/jquery.blockUI.min.js
    /assets/js/frontend/woocommerce.min.js
    /assets/js/jquery-cookie/jquery.cookie.min.js
    /assets/js/frontend/cart-fragments.min.js
    /assets/js/frontend/country-select.min.js
    /assets/js/frontend/address-i18n.min.js

    And my theme is also loading these two js for woocommerce, but I would probably need to ask my theme’s support how to dequeue them…

    theme/assets/js/min/add-to-cart-variation-min.js
    theme/assets/js/min/wc-quantity-increment.min.js

    Thank you,
    Andrea

    • Milan says:

      Hi Andrea,

      Below is the possible solution for your case.

      Couple of notes:

      1) wc-quantity-increment.min.js script concerns quantity increment buttons and these were depreciated as of WooCommerce 2.3. In the snippet below it should be dequeued with line wp_dequeue_script( ‘wcqi-js’ ); .

      2) Also I didn’t tested this solution so give it a try and let us know if it worked for you.

      Many thanks,

      Milan


      add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );

      function child_manage_woocommerce_styles() {
      remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );
      if ( !is_cart() && !is_checkout && !is_page(4686)) ) {
      wp_dequeue_style( 'select2' );
      wp_dequeue_script( 'wc-add-to-cart' );
      wp_dequeue_script( 'select2' );
      wp_dequeue_script( 'jquery-blockui' );
      wp_dequeue_script( 'woocommerce' );
      wp_dequeue_script( 'jquery-cookie' );
      wp_dequeue_script( 'wc-cart-fragments' );
      wp_dequeue_script( 'wc-country-select' );
      wp_dequeue_script( 'wc-address-i18n' );
      wp_dequeue_script( 'wc-add-to-cart-variation' );
      wp_dequeue_script( 'wcqi-js' );
      }}

      • Andrea says:

        Hi Milan,
        thank you, but unfortunately this did not work either. First of all, you have one too many brackets, so I had to remove that, but the scripts were still loading. I did some more digging though and found a solution! It won’t work with dequeue, but with deregister. So you just need to change that and it works.

        Thanks though! Now I finally got it solved .)

        • Milan says:

          I’m glad that you managed to remove scripts with deregister and also earlier script has all brackets that it needs because you have to make sure to close the ‘if’ statements correctly :)

          Anyway thanks for the input and I hope that it will help someone with similar issue.

          Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

 

wegge118@mailxu.com