Disable WooCommerce Cart Fragments Selectively (Code-Based Solution)

design
Disable WooCommerce cart fragments selectively using PHP to reduce AJAX requests and improve site performance

If you’ve ever opened DevTools and wondered why your WooCommerce site keeps firing background AJAX requests—even on simple content pages—the answer is usually cart fragments.

The problem isn’t cart fragments themselves. The problem is that WooCommerce loads them everywhere, even when the cart is empty and no real-time cart updates are needed. On cached or performance-focused stores, this can quietly kill speed scores and add unnecessary server load.

In this guide, you’ll learn how to disable WooCommerce cart fragments selectively, using safe, code-based rules that improve performance without breaking your mini cart or checkout flow.


What WooCommerce Cart Fragments Actually Do

WooCommerce uses a script called wc-cart-fragments.js to keep cart data in sync across pages.

It does this by triggering an AJAX request:

?wc-ajax=get_refreshed_fragments

This request returns updated HTML snippets (“fragments”) such as:

  • Mini cart contents
  • Cart item count
  • Cart totals

That’s useful when:

  • A customer adds a product via AJAX
  • You want the header cart count to update instantly
  • The user stays on the same page after adding to cart

But here’s the catch…


Why Cart Fragments Hurt Performance (Especially on Cached Sites)

By default, WooCommerce loads cart fragments on:

  • Home pages
  • Blog posts
  • Static pages
  • Landing pages
  • Category archives

Even when:

  • The cart is empty
  • No mini cart is visible
  • No AJAX add-to-cart is used

This causes:

  • Extra AJAX calls on every page load
  • Cache bypassing (many caching layers can’t fully cache fragment requests)
  • Increased admin-ajax.php or wc-ajax traffic
  • Slower TTFB and page load times

If you’ve ever optimized a WooCommerce site and still saw random AJAX requests in the Network tab, cart fragments are usually the culprit.


When You Should NOT Disable Cart Fragments

Before touching anything, it’s important to know when cart fragments are actually required.

You should keep cart fragments enabled if:

  • Your header shows a live cart count
  • You use a dropdown mini cart
  • Customers add products via AJAX and stay on the same page
  • Your theme relies on real-time cart updates

Blindly disabling fragments everywhere often leads to:

  • Cart count not updating
  • Mini cart showing stale data
  • “Why doesn’t my cart update?” complaints

That’s why selective disabling is the correct approach.


The Best Strategy: Disable Cart Fragments Selectively

Instead of removing cart fragments globally, the safest strategy is:

  1. Disable cart fragments on pages that don’t need them
  2. Keep them enabled on Cart and Checkout
  3. Optionally keep them enabled only when the cart is not empty

This gives you:

  • Faster browsing for new visitors
  • Accurate cart data for shoppers
  • Fewer background requests overall

Let’s walk through the best implementations.


Method 1 (Recommended): Disable Cart Fragments When Cart Is Empty

This is the most balanced solution for performance and UX.

WooCommerce sets a cookie called woocommerce_cart_hash when a cart contains items. We can use that to decide whether cart fragments are needed.

Code: Disable Fragments Unless Cart Has Items

Add this to your child theme’s functions.php or a small custom plugin:

<?php
/**
 * Disable WooCommerce cart fragments selectively.
 * Keeps fragments on cart/checkout and when cart has items.
 */
add_action('wp_enqueue_scripts', function () {

    // Ensure WooCommerce functions exist
    if ( ! function_exists('is_woocommerce') ) {
        return;
    }

    // Always keep fragments on cart and checkout
    if ( is_cart() || is_checkout() ) {
        return;
    }

    // Check if cart hash cookie exists (cart likely has items)
    $has_cart_hash = isset($_COOKIE['woocommerce_cart_hash']) && ! empty($_COOKIE['woocommerce_cart_hash']);

    // If cart has items, keep fragments enabled
    if ( $has_cart_hash ) {
        return;
    }

    // Otherwise, disable cart fragments
    wp_dequeue_script('wc-cart-fragments');
    wp_deregister_script('wc-cart-fragments');

}, 200);

Why This Works

  • New visitors don’t load unnecessary AJAX requests
  • Cached pages stay truly cached
  • Active shoppers still get real-time cart updates
  • Cart and checkout remain 100% safe

This approach mirrors how many performance plugins handle WooCommerce internally.


Method 2: Disable Cart Fragments Everywhere Except WooCommerce Pages

If you want a simpler rule-based approach (no cookie checks), this method works well.

Code: Disable Fragments on Non-Woo Pages

add_action('wp_enqueue_scripts', function () {

    if ( ! function_exists('is_woocommerce') ) {
        return;
    }

    // Keep fragments on WooCommerce-related pages
    if (
        is_woocommerce() ||
        is_cart() ||
        is_checkout() ||
        is_account_page()
    ) {
        return;
    }

    wp_dequeue_script('wc-cart-fragments');
    wp_deregister_script('wc-cart-fragments');

}, 200);

When to Use This

  • Your header cart is only visible on WooCommerce pages
  • You don’t rely on cart counts site-wide
  • You want a simple and predictable rule

Optional: Reduce the Need for Cart Fragments Entirely

In WooCommerce settings, you can reduce reliance on AJAX cart behavior:

  • Disable “Enable AJAX add to cart buttons on archives”
  • Enable “Redirect to the cart page after successful addition”

This forces a page reload after add-to-cart, making cart fragments less critical.

This isn’t ideal for every store, but it’s worth considering for performance-focused setups.


How to Verify Cart Fragments Are Disabled Correctly

After adding your code:

  1. Open Chrome DevTools → Network tab
  2. Reload a content page (homepage, blog post)
  3. Search for:
    • get_refreshed_fragments
    • wc-ajax=get_refreshed_fragments

Expected Results

  • ❌ No fragment request on content pages (cart empty)
  • ✅ Fragment request appears on Cart or Checkout
  • ✅ Fragment request appears once items are added (Method 1)

If you still see fragment calls everywhere, clear caches and test again.


Common Problems (and Their Real Causes)

Mini Cart Count Stops Updating

This means cart fragments were disabled while the cart had items.
Fix: Use the cookie-based method (Method 1).

Cart Empties After Page Refresh

That’s usually a cookie, caching, or session issue—not cart fragments themselves.
WooCommerce Cart Empty After Page Refresh

admin-ajax.php 400 or 403 Errors

Cart fragments increase AJAX volume, but 400/403 errors usually point to:


Why This Matters for Performance

On high-traffic or cached WooCommerce sites, selectively disabling cart fragments can:

  • Reduce background requests
  • Improve Core Web Vitals
  • Lower server load
  • Stabilize caching behavior

It’s not a magic fix—but it’s one of the highest-impact performance tweaks you can safely make.

FAQs: Disable WooCommerce Cart Fragments Selectively

WooCommerce cart fragments update cart-related elements like the mini cart and cart count using AJAX. It sends a background request (wc-ajax=get_refreshed_fragments) to keep cart data in sync without reloading the page.

Yes, if you disable them selectively. Removing cart fragments globally can break mini cart updates, but disabling them only on non-cart pages or when the cart is empty is safe and recommended for performance.

You can dequeue the wc-cart-fragments script using PHP while keeping it enabled on cart and checkout pages, or only when the cart has items by checking the woocommerce_cart_hash cookie.

WooCommerce loads cart fragments site-wide by default to ensure cart data stays updated everywhere, even though most pages don’t actually need real-time cart updates.

Yes. Disabling WooCommerce cart fragments selectively can reduce unnecessary AJAX requests, improve caching efficiency, and lower server load—especially on content-heavy or high-traffic sites.

Disabling cart fragments everywhere may cause the cart count or mini cart to stop updating until a page refresh, leading to poor user experience. That’s why selective disabling is preferred.

This usually means cart fragments are still enabled on the current page, the cart contains items, or caching hasn’t been cleared. Clear all caches and test again with an empty cart.

Indirectly. Cart fragments increase AJAX traffic, but 400 or 403 errors are typically caused by security rules, caching layers, or server configurations—not the fragments script itself.

No. Cart fragments should always remain enabled on the checkout page to ensure totals, fees, and cart data stay accurate during the purchase process.

The safest and most effective approach is disabling cart fragments only when the cart is empty, while keeping them enabled on cart and checkout pages and when items are added.

Meet the Author

Babar khan

Babar Ilyas is a full-stack WordPress developer and SEO strategist focused on building fast, functional, and search-optimized websites. With years of hands-on experience, he shares real-world fixes and dev workflows that actually work.
When he’s not deep in code, he’s dropping fresh blog posts and tracking what Google’s up to — one ranking at a time.
Leave a Reply

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

    Branding Design Development Front-End Website-Redesigning Shopify-Development WordPress-Development
    Branding Design Development Front-End Website-Redesigning Shopify-Development WordPress-Development
    We love crafting unforgettable
    digital experiences, brands and websites with people like you.
    Follow us:
    Let’s get started
    We'd love to hear about your project.
    © 2025 babarilyas. All rights reserved.