☰
WORDPRESS / PRACTICE

How to Optimize WooCommerce Cart Fragments Without Breaking the Cart

How to Optimize WooCommerce Cart Fragments Without Breaking the Cart

WooCommerce can load the `wc-cart-fragments.js` JavaScript file on many pages across a site. The script periodically checks the cart state and sends AJAX requests to `?wc-ajax=get_refreshed_fragments` to update the mini-cart, item count, and order total.

If the mini-cart is not used on a particular page, these requests may be unnecessary. This is especially noticeable on high-traffic sites, where every additional PHP request adds load to the server, object cache, and database. However, blindly disabling Cart Fragments globally can break the mini-cart or custom cart logic.

What Are WooCommerce Cart Fragments?

Cart Fragments are a WooCommerce mechanism for dynamically updating parts of a page related to the cart. This most commonly includes the classic mini-cart widget: the item count, product list, and total amount.

After a product is added through AJAX, WooCommerce can update these elements without fully reloading the page. To do this, the browser sends a request to a dedicated endpoint and receives HTML fragments that must be replaced in the DOM.

The problem occurs when the script loads on pages that do not contain a mini-cart or another component that uses this mechanism. In that case, the site may make periodic requests without providing any practical benefit to the visitor.

  • the file usually responsible for this functionality is `wc-cart-fragments.js`;
  • the typical AJAX request is `?wc-ajax=get_refreshed_fragments`;
  • the primary optimization candidates are pages without a mini-cart or related custom components.

Why You Should Not Disable the Script Globally

Removing `wc-cart-fragments` globally may seem like a simple solution, but it does not account for the actual structure of the store. A mini-cart may not be visible in the main template on one page, yet still open through a mobile menu, sidebar, header, or popup.

In addition to the standard WooCommerce widget, Cart Fragments may be used by themes, plugins, custom templates, and custom scripts. Before optimizing, determine exactly where the cart needs to be updated dynamically.

Cart Fragments should also not be confused with the functionality of the cart and checkout pages themselves. Disabling a specific JavaScript file should not be applied mechanically to every WooCommerce scenario.

  • do not remove the script without checking the mini-cart on both desktop and mobile devices;
  • account for mini-carts in the header, menu, drawer panel, and popup;
  • check whether the theme or a third-party plugin loads or calls the script.

Safely Disable It Selectively Through functions.php

A practical approach is to keep Cart Fragments on the cart page, checkout page, and any page where the standard mini-cart widget is active. On other pages, the script can be dequeued.

Add the code to a child theme or a small custom functionality plugin. Avoid placing it directly in the parent theme files, because a theme update may overwrite your changes.

  • create a backup or prepare a quick rollback method;
  • add the code in an environment where it can be tested safely;
  • a priority of `100` helps run the dequeue after most standard script enqueues.
add_action('wp_enqueue_scripts', function () {
    if (
        ! is_cart() &&
        ! is_checkout() &&
        ! is_active_widget(false, false, 'woocommerce_widget_cart', true)
    ) {
        wp_dequeue_script('wc-cart-fragments');
    }
}, 100);

How This Check Works

`is_cart()` and `is_checkout()` keep the script enabled on the key store pages. The `is_active_widget()` check determines whether the standard WooCommerce widget with the ID `woocommerce_widget_cart` is active in a registered widget area.

The condition uses AND logic: the script is removed only when the current page is not the cart, is not the checkout, and does not have the standard mini-cart widget active.

  • if the mini-cart is implemented as a block or custom template, this check may not detect it;
  • if the theme enqueues the script after this code runs, a different solution or a later hook may be required;
  • for a non-standard mini-cart, first determine its dependencies.

When This Approach Needs to Be Modified

The example above is designed for the classic WooCommerce widget. It is not a universal way to detect every cart component. A modern theme may use WooCommerce blocks, a custom AJAX endpoint, or JavaScript that depends on Cart Fragments only partially.

If the item count disappears from the header, the total no longer updates, or the mini-cart stops opening after the script is disabled, restore the script on the relevant pages. For a custom component, you can keep `wc-cart-fragments.js` enabled or implement a separate endpoint that returns only the required data.

Do not add every page to the exceptions without analysis. Instead, identify the specific templates where customers interact with the cart and preserve the functionality there.

  • check the theme and plugin documentation if the mini-cart is not a standard widget;
  • do not remove script dependencies if third-party code expects them to be available;
  • for complex stores, consider conditionally loading custom JavaScript instead of using general Cart Fragments.

How to Test the Site After Making Changes

Testing should be performed as a guest and in a new browser session. Cached cookies and previously loaded scripts can make the site appear to work even though a real visitor may experience different behavior.

Do not check only the home page. Test every template where a customer can add a product or open the cart.

  • add a product to the cart from both the catalog and the product page;
  • check quantity updates, product removal, and navigation to checkout;
  • test applying and removing a coupon;
  • open the mini-cart on desktop and mobile devices;
  • test variable products if the store sells them;
  • make sure the cart does not lose data after a user logs in or changes the currency.

Checking in DevTools

Open the browser Developer Tools, go to the Network tab, and reload the page. In the request filter, search for `get_refreshed_fragments` or `wc-ajax`.

On pages without a mini-cart, unnecessary requests should disappear. On pages where dynamic cart updates are required, they may remain, which is expected behavior.

  • also check the Console tab for JavaScript errors;
  • make sure `wc-cart-fragments.js` is not required by another component;
  • test pages after clearing the page cache, CDN cache, and browser cache.

Common Optimization Mistakes

The most common mistake is removing `wc-cart-fragments` everywhere after noticing a few unnecessary requests in the Network tab. If the site has a mini-cart, the performance issue should be addressed by defining more precise loading conditions, not by breaking the feature.

Another mistake is testing changes only as an administrator. An authenticated administrator may see a different version of the page because of caching, additional admin bars, or different theme logic. The primary scenario must be tested as a regular visitor.

After changing the code, it is important to clear page caches. Otherwise, some users may receive the old HTML version while others receive the new one, making troubleshooting more difficult.

  • do not rely only on a PageSpeed score without checking functionality;
  • do not copy the code without analyzing the custom header and mobile menu;
  • do not edit WooCommerce plugin files directly;
  • do not treat the absence of a single AJAX request as proof that the entire cart works correctly.

Frequently asked questions

Can WooCommerce Cart Fragments be disabled completely?

Yes, but only if the site does not use a classic or custom mini-cart that depends on this script. For most stores, it is safer to disable it selectively on pages where it is not needed.

Should Cart Fragments be disabled on the cart page?

The cart page should usually remain in the exceptions. Customers change quantities, remove products, and apply coupons there, so any intervention must be tested especially carefully.

Why did the mini-cart stop updating after dequeue?

The mini-cart or custom component most likely uses `wc-cart-fragments.js`, even if the standard widget is not detected by `is_active_widget()`. Restore the script and check the dependencies of the theme, blocks, and plugins.

Will all WooCommerce requests disappear after disabling Cart Fragments?

No. Other WooCommerce AJAX requests, checkout requests, variation updates, and custom theme logic may remain. The goal of this optimization is to remove only unnecessary Cart Fragments requests.

Conclusion

WooCommerce Cart Fragments optimization should be conditional rather than global. First identify where the mini-cart is used on the site, then disable the script only on pages without that functionality.

After implementation, test the store as a guest: product additions, quantities, coupons, the mini-cart, and checkout. In DevTools, confirm that `get_refreshed_fragments` requests disappear only where they are genuinely unnecessary.

← All WordPress materials