WooCommerce PHP Intermediate

Remove WooCommerce Scripts on Non-Shop Pages

Last updated: May 6, 2026

WooCommerce loads its CSS and JavaScript files on every single page of your WordPress site by default, your homepage, blog posts, contact page, about page, and every other page that has nothing to do with shopping. This includes the cart fragments script, which fires an AJAX request on every page load to keep the cart count updated, even on pages where no cart UI exists. For sites where the store is a secondary feature, this represents measurable and entirely avoidable overhead.

This snippet removes WooCommerce’s front-end assets on all pages where they aren’t needed, while leaving them fully intact on shop pages, product pages, the cart, checkout, and account pages.

The Code

Add this to your functions.php or a site-specific plugin. It hooks into wp_enqueue_scripts at priority 99, after WooCommerce has registered and enqueued its assets at its default priority, and dequeues the ones that are unnecessary on non-commerce pages.

The Conditional Check

The early return condition combines four WooCommerce-specific template functions. is_woocommerce() returns true on any WooCommerce template, shop archive, product category, product tag, and single product pages. is_cart() and is_checkout() cover the cart and checkout pages. is_account_page() covers the My Account section where customers view orders, manage addresses, and handle subscriptions.

If any of these return true, the function exits and WooCommerce assets load normally. Only pages outside all of these contexts have the assets removed.

The wc-cart-fragments Script

The cart fragments script deserves specific mention because it’s both the most impactful asset to remove and the one most sites never think about. On every page load, this script fires an AJAX request to /wp-admin/admin-ajax.php to retrieve the current cart state and update the cart count in the header. On pages with no cart UI, this is a completely wasted round-trip. Removing it from non-shop pages eliminates the AJAX call and the associated server load entirely, a meaningful win on high-traffic sites.

Mini-Cart in the Header

If your theme displays a mini-cart or cart item count in the header across all pages, the cart fragments script is what keeps that count live without a page refresh. Removing wc-cart-fragments on non-shop pages means the cart count in the header won’t update dynamically on those pages. The count will still be accurate on page load based on the session cookie, it just won’t update in real time via AJAX. For most sites this is an acceptable trade-off.

Testing After Implementation

After adding this snippet, verify your shop, product, cart, and checkout pages work correctly. Use your browser’s Network tab to confirm WooCommerce assets are absent on a blog post and present on a product page. If any functionality breaks on non-shop pages, for example, an add-to-cart button embedded in a blog post, you’ll need to add those specific page templates back to the exception condition.

functions.php
add_action( 'wp_enqueue_scripts', function() {
    if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) {
        return;
    }

    // Remove WooCommerce styles
    wp_dequeue_style( 'woocommerce-general' );
    wp_dequeue_style( 'woocommerce-layout' );
    wp_dequeue_style( 'woocommerce-smallscreen' );
    wp_dequeue_style( 'woocommerce_frontend_styles' );
    wp_dequeue_style( 'woocommerce-inline' );
    wp_dequeue_style( 'wc-block-style' );

    // Remove WooCommerce scripts
    wp_dequeue_script( 'woocommerce' );
    wp_dequeue_script( 'wc-cart-fragments' );
    wp_dequeue_script( 'wc-add-to-cart' );
    wp_dequeue_script( 'jquery-cookie' );
}, 99 );

Built by Nahnu Plugins

Need something more powerful than a snippet?

Our commercial plugins go further, built for serious WordPress sites with full support, updates, and documentation included.

Browse All Plugins →

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only