Disable WooCommerce Scripts and Styles on Non-Shop Pages to Improve Performance

Load WooCommerce scripts and styles only on shop pages

WooCommerce enqueues its stylesheets and several scripts on every page of the site, including the blog. The best-known one, wc-cart-fragments, sends an AJAX request on page load to refresh the mini-cart, which also defeats some page caches. On a site where the shop is one section among others, these files can be removed outside the shop.

Problem: Blog posts and landing pages load WooCommerce CSS and JavaScript they never use, and every page view triggers a ?wc-ajax=get_refreshed_fragments request.

Solution: On wp_enqueue_scripts with a late priority, check whether the page is a WooCommerce page (is_woocommerce(), is_cart(), is_checkout(), is_account_page()), and if not, dequeue the WooCommerce styles and scripts by their handles. Keep the cart fragments where the header shows a mini-cart.

The code

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

function ha_wc_assets_only_in_shop() {
    if ( ! function_exists( 'is_woocommerce' ) ) {
        return;
    }
    if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) {
        return;
    }
    if ( is_singular() && has_shortcode( get_post()->post_content, 'products' ) ) {
        return; // a page with a [products] shortcode is a shop page too
    }

    foreach ( [ 'woocommerce-general', 'woocommerce-layout', 'woocommerce-smallscreen' ] as $handle ) {
        wp_dequeue_style( $handle );
    }
    foreach ( [ 'woocommerce', 'wc-add-to-cart', 'wc-cart-fragments' ] as $handle ) {
        wp_dequeue_script( $handle );
    }
}

is_woocommerce() covers the shop page, product categories and tags, and single products, but not the cart, checkout and account pages, which is why they are checked separately. Priority 99 runs after WooCommerce has enqueued its files.

Only dequeue, do not deregister: other plugins may declare these handles as dependencies, and a deregistered dependency silently removes their scripts as well.

Remove the styles everywhere

If the theme styles the shop itself, WooCommerce has a filter for its stylesheets:

add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );

The mini-cart

A cart icon with a counter in the header needs wc-cart-fragments on every page, or it shows a stale number from the page cache. Either keep the script, or show the counter only on shop pages and a plain “Cart” link elsewhere.

Check the result

Open a blog post with the browser’s network panel: no woocommerce files and no get_refreshed_fragments request should appear. Then test adding to cart on a product, on a category page and from a page with the [products] shortcode.

NOTE: Block-based shop pages load the styles of the WooCommerce blocks only where these blocks are used, so this code is mainly for classic themes and shortcodes. Remove the handles one by one and test after each change, not all at once.

Update (2023): WooCommerce 7.8 (June 2023) stopped loading wc-cart-fragments on every page by default; it is loaded only where the Cart widget is present. On current versions the fragments request is usually gone already, and the code above matters mostly for the stylesheets.