search ]

Prevent Loading WooCommerce’s JS & CSS Files & Improve Loading Time

Building an online store using WooCommerce isn’t rocket science. You can either do it yourself or use plenty of guides on WooCommerce available on the Savvy Blog and across the web. However, to succeed, your store must provide at least a good user experience. A significant part of that user experience is the page load time and the responsiveness of the store.

Your online store (your business, in other words) can fail simply due to a poor user experience. In this post, we’ll discuss one of the many actions you can take to improve the load time of websites using WooCommerce.

This specific action involves how to prevent WooCommerce from loading unnecessary JavaScript and CSS files on all pages of your site, restricting them only to pages where WooCommerce is active.

Things to Consider Before You Proceed:

  • Does your site have elements outside WooCommerce pages (e.g., cart, checkout, product pages) that rely on these scripts? Examples might include a cart widget in the header or a widget in the sidebar throughout the site.
  • Do you have a staging or development environment? It’s highly recommended to test these changes locally on your computer or on a staging site if available.
  • You should be comfortable with coding and know what you’re doing.
  • Always ensure you have a proper backup of your site before making any code changes.

Removing Default WooCommerce Files

By default, WooCommerce loads three CSS files and five JavaScript files. These include:

<!--CSS Files-->
<link href='.../plugins/woocommerce/assets/css/woocommerce-layout.css' rel='stylesheet' type='text/css' />
<link href='.../plugins/woocommerce/assets/css/woocommerce-smallscreen.css' rel='stylesheet' type='text/css' />
<link href='.../plugins/woocommerce/assets/css/woocommerce.css' rel='stylesheet' type='text/css' />

<!--JavaScript Files-->
<script src='.../plugins/woocommerce/assets/js/jquery-blockui/jquery.blockUI.min.js'></script>
<script src='.../plugins/woocommerce/assets/js/frontend/add-to-cart.min.js'></script>
<script src='.../plugins/woocommerce/assets/js/js-cookie/js.cookie.min.js'></script>
<script src='.../plugins/woocommerce/assets/js/frontend/woocommerce.min.js'></script>
<script src='.../plugins/woocommerce/assets/js/frontend/cart-fragments.min.js'></script>

CSS file names will end with rtl if your WooCommerce store uses a right-to-left language like Hebrew.

For instance, there’s no reason to load the add-to-cart.min.js script on your Contact page, right? Similarly, there’s no need to load the woocommerce-layout.css file on your blog posts.

You can check your site’s source code on any page to confirm that these files are loading. If your site has already undergone optimization, you shouldn’t see these files except on WooCommerce-specific pages.

To ensure these files are loaded only on relevant store pages – such as product pages, checkout pages, or the cart – you can add the following code to your functions.php file:

<?php
//* Enqueue scripts and styles

function savvy_remove_woocommerce_css_js() {

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

        // Check if we are on a WooCommerce page
        if(! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) {

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

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

            wp_deregister_script( 'jquery-blockui' );
            wp_dequeue_script( 'jquery-blockui' );

            wp_deregister_script( 'js-cookie' );
            wp_dequeue_script( 'js-cookie' );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'savvy_remove_woocommerce_css_js', 99 );

Note the conditional tags on line 10. We also include is_account_page() so that WooCommerce assets still load on the My Account page. If you need to load these scripts on a non-WooCommerce page, you can adjust these conditions accordingly.

If you have questions about this topic, feel free to ask in the comments, and I’ll reply as soon as possible.

Appendix A

This solution works well for both CSS and JavaScript files. However, if you want to completely remove WooCommerce’s CSS files from all pages, including WooCommerce pages, you can use the following filter:

// Remove all stylesheets in one line
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );

Alternatively, if you wish to remove specific CSS files, you can do so like this:

// Remove each style one by one
function sv_dequeue_styles( $enqueue_styles ) {
    unset( $enqueue_styles['woocommerce-general'] );    // Remove the gloss
    unset( $enqueue_styles['woocommerce-layout'] );     // Remove the layout
    unset( $enqueue_styles['woocommerce-smallscreen'] ); // Remove the smallscreen optimization
    return $enqueue_styles;
}

add_filter( 'woocommerce_enqueue_styles', 'sv_dequeue_styles' );

This approach is discussed in detail in the post How to Remove Default WooCommerce Stylesheets.

The cart-fragments.min.js Performance Problem

Among all WooCommerce scripts, cart-fragments.min.js deserves special attention. This script fires an AJAX request (?wc-ajax=get_refreshed_fragments) on every page load to keep the mini-cart widget updated. Even when the cart is empty, this request runs and can be one of the slowest requests on your site.

Starting with WooCommerce 7.8, the cart fragments script is only enqueued on pages that actually render a cart widget. If you are running WooCommerce 7.8 or later, this issue may already be resolved for your store. Check your page source to confirm.

If you are on an older version or still see the fragments request on non-cart pages, you can disable it selectively. The smartest approach is to only load cart fragments when the user actually has items in the cart:

add_action( 'wp_enqueue_scripts', function() {
    if ( ! isset( $_COOKIE['woocommerce_items_in_cart'] ) || $_COOKIE['woocommerce_items_in_cart'] === '0' ) {
        wp_dequeue_script( 'wc-cart-fragments' );
    }
}, 99 );

This preserves cart functionality for shoppers while eliminating the AJAX overhead for everyone else.

WooCommerce Block Styles

Modern WooCommerce versions also load block-related stylesheets that may not be needed on every page. These include handles like wc-blocks-style and wc-blocks-vendors-style. If you are not using WooCommerce’s Gutenberg blocks on non-store pages, you can dequeue these as well:

add_action( 'wp_enqueue_scripts', function() {
    if ( function_exists( 'is_woocommerce' ) && ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) {
        wp_dequeue_style( 'wc-blocks-style' );
        wp_dequeue_style( 'wc-blocks-vendors-style' );
    }
}, 99 );

For a broader look at speeding up your WordPress site, check out the guide on proven strategies to optimize WordPress loading time. You can also load plugins selectively with Plugin Organizer for even more control over which assets load where.

FAQs

Common questions about WooCommerce asset optimization:

Will removing WooCommerce scripts break my cart widget?
It depends on where the cart widget is displayed. If you have a mini-cart in the header that appears on every page, dequeuing wc-cart-fragments globally will prevent it from updating dynamically. In that case, either keep cart fragments enabled on all pages or use a cookie-based approach that only loads the script when items are in the cart.
Why should I use priority 99 in the add_action call?
WooCommerce enqueues its scripts at the default priority (10). By setting your dequeue function to priority 99, you ensure it runs after WooCommerce has already registered and enqueued its assets. Without a higher priority, your dequeue calls may execute before the scripts are enqueued and have no effect.
Does WooCommerce 7.8+ still load cart fragments on every page?
No. Starting with WooCommerce 7.8, the cart fragments script is only enqueued on pages that render a cart or mini-cart widget. This was a major performance improvement. If you are running WooCommerce 7.8 or later, check your page source to confirm it is no longer loading on non-cart pages before applying manual dequeue code.
Can I use a plugin instead of adding code to functions.php?
Yes. Performance plugins like Perfmatters, Asset CleanUp, and Plugin Organizer offer UI-based controls to selectively disable WooCommerce scripts and styles per page or post type. These are good options if you prefer not to write code.
Is it safe to deregister jquery-blockui and js-cookie?
On non-WooCommerce pages, yes. These libraries are used by WooCommerce for AJAX cart blocking overlays and cookie management. Other plugins may also depend on them, so if you notice issues on non-WooCommerce pages after deregistering, check whether another plugin requires these scripts and adjust your conditions accordingly.

Summary

WooCommerce loads several CSS and JavaScript files on every page of your site by default, even where they are not needed. By dequeuing these assets on non-WooCommerce pages using conditional tags, you can reduce unnecessary HTTP requests and improve page load times across your store.

Pay special attention to cart-fragments.min.js, which fires an AJAX request on every page load to update the mini-cart. If you are running WooCommerce 7.8 or later, this is already handled. For older versions, a cookie-based conditional approach gives you the best balance between performance and functionality.

Join the Discussion
4 Comments  ]
  • Assaf 18 January 2024, 15:09

    Shalom Roee,

    Great post. If I’m using Elementor then I can remove css files altogether no? I mean even woocommerce pages are handled by elementor.

    Also regarding elementor – I don’t really have an add to cart. When someone wants to buy he sent to immediately to the checkout page using something like www.**mysite**.com/checkout/?add-to-cart=**product-number**

    So I think that in this case the js cookie of adding to cart could be removed altogether? To note that I don’t have a store with thousands of product. Just a few online courses.

    Regarding the jquery cookie – I’m using an affiliate plugin (affiliatewp) so I think that if you unload the js cookie site-wide it will also effect it and it should not be removed.

    • Roee Yossef 18 January 2024, 17:17

      I’m not really sure about the behaviour of Elementor, but it seems reasonable that you can remove all the CSS. You should give a try (and let us know 🙂 )

      If you do not use the Cart you can definitely remove the relevant assets, but i do not know if there are any dependencies on the cookie js for affiliatewp. once again, you should check it locally as i don’t have experience with affiliatewp and neither with elementor…

  • Maggew.com 25 January 2024, 8:19

    Yea this snippet was beautiful. Thank you sir for the excellent code snippet.

    On a side note. I should be able to click comment at very top of post and smooth scroll down here. Maybe a feature you’ll integrate soon.

    • Roee Yossef 25 January 2024, 10:48

      Thanks Matt, Will soon apply your recommendation.. 🙂

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo