WooCommerce

WooCommerce Cart Fragments Performance: Cost, Trade-offs and Alternatives

WooCommerce cart fragments performance comes down to one extra request: `admin-ajax.php` fires on every page load to refresh the mini-cart, booting WordPress, recalculating the cart and returning HTML you often do not need. I disable it on catalog and landing routes, keep a server-rendered count elsewhere, and measure the result in LCP, INP and CLS rather than page weight.

By Saif Al Jahanwebdev-saif.com

What cart fragments actually do on a classic storefront

On a classic WooCommerce theme, `wc-cart-fragments.js` runs on almost every front-end request. Its localized settings (`wc_cart_fragments_params`) contain the `admin-ajax.php` URL, a nonce and a cart-hash key. After `DOMContentLoaded` the script posts `action=woocommerce_get_refreshed_fragments`, WooCommerce calls `WC()->cart->calculate_totals()`, and the `woocommerce_add_to_cart_fragments` filter returns an object of CSS selectors mapped to replacement HTML. The script then swaps those nodes and fires `wc_cart_fragments_refresh`. Newer versions keep a session-storage copy and a `wc_cart_hash` cookie to skip duplicate requests, but the first view of a session still pays for the full round trip. That is the request most performance audits never attribute to the cart.

  • Endpoint: POST to `admin-ajax.php` with `action=woocommerce_get_refreshed_fragments`
  • Filter: `woocommerce_add_to_cart_fragments` returns selector-to-HTML pairs
  • Client: `wc-cart-fragments.js` swaps nodes and fires `wc_cart_fragments_refresh`
  • State: `wc_cart_hash` cookie and session storage suppress repeat calls

What the AJAX endpoint really costs

The cost is not the JSON payload; it is the WordPress bootstrap behind it. `admin-ajax.php` loads the full plugin stack, resolves the session, recalculates cart totals and runs every callback attached to the fragment filter. Because the request is a POST with a nonce, full-page caches usually bypass it and the origin answers every anonymous visit. A single plugin adding a product query or a `WP_Query` loop inside its fragment callback can turn a 30 ms response into several hundred. On the client, the response is parsed and injected after the main content has already painted, so it competes with LCP, adds long tasks that hurt INP, and shifts the header if the mini-cart markup changes height. In my builds I treat it as a render-blocking-adjacent request even though it is asynchronous.

  • Watch the `admin-ajax.php` row after `DOMContentLoaded` in the Network panel
  • Count database queries with Query Monitor while the fragment request runs
  • Measure response TTFB separately from the main document TTFB
  • Check header shift when the mini-cart HTML is replaced
  • Inspect every `woocommerce_add_to_cart_fragments` callback added by plugins

When I disable cart fragments

I disable fragments when the mini-cart does not need to change without a navigation. Brochure catalogues, lead-generation landing pages, stores with checkout on a separate domain, and templates that render no cart widget all qualify. So do high-traffic pages where the origin is already near its TTFB budget and there is no persistent object cache to absorb repeated cart calculations. If the store uses cart and checkout blocks backed by the Store API (`wc/store/v1/cart`), classic fragments are usually redundant. I keep them when customers add to cart from product archives and expect an instant badge update without leaving the page, and when a third-party mini-cart relies on the refresh event. The decision is per template, not per site, whenever the storefront mixes catalogue and shop routes.

  • Catalogue or brochure routes with no cart widget
  • Landing pages whose add-to-cart links redirect to an external checkout
  • Headless or Store API block themes using `wc/store/v1/cart`
  • Traffic spikes where origin TTFB is already the limiting metric
  • Templates where a static server-rendered count is accurate enough

How to disable them without breaking add to cart

Simply removing the script tag is not enough. WooCommerce enqueues `wc-cart-fragments` as a dependency of AJAX add-to-cart, so a naive deregistration can leave buttons that do nothing or a badge that never updates. I dequeue it from `wp_enqueue_scripts` at a late priority, only on the routes identified above, and check that no other plugin re-enqueues it. Where products still have AJAX add-to-cart buttons, I either let them redirect by filtering `woocommerce_add_to_cart_redirect` and returning `wc_get_cart_url()`, or disable the AJAX add-to-cart behaviour so the form does a normal POST. Then I remove or cheapen my own `woocommerce_add_to_cart_fragments` callbacks, because a filter with no client to consume it still executes on the remaining requests.

  • Dequeue on `wp_enqueue_scripts` with priority later than WooCommerce
  • Scope by `is_page()`, `is_product()` or a template condition
  • Route add-to-cart through `woocommerce_add_to_cart_redirect` when fragments are gone
  • Test logged-in and logged-out sessions, plus cart and checkout pages

Lighter alternatives and fragment caching

When a live badge is genuinely needed, I replace the full fragment set with the smallest possible source of truth. The cheapest is server-rendered HTML: print `WC()->cart->get_cart_contents_count()` in the header and let the next navigation refresh it. For an instant visual update, WooCommerce already sets a `woocommerce_items_in_cart` cookie, so JavaScript can increment a counter on the add-to-cart event and reconcile on the next page load — no request at all. If an endpoint is unavoidable, I narrow `woocommerce_add_to_cart_fragments` to one counter node, wrap expensive calculations in `wp_cache_get`/`wp_cache_set` keyed by the cart hash, and prefer the Store API `wc/store/v1/cart` route with client-side caching. A cache-friendly GET with an ETag is viable for logged-out visitors, but I never drop the nonce or cache logged-in fragments.

  • Server-render `WC()->cart->get_cart_contents_count()` in the header
  • Use the `woocommerce_items_in_cart` cookie for instant badge updates
  • Narrow the fragment filter to a single counter selector
  • Cache computed HTML with `wp_cache_get`/`wp_cache_set` keyed by cart hash
  • Consider `wc/store/v1/cart` with in-memory client caching for block themes

How I measure the result

I record a DevTools waterfall before and after the change on a throttled 4G profile with a mid-range Android CPU preset, then compare the same route logged in and logged out. The checks are simple: no `admin-ajax.php` fragment request on routes where I disabled it, no JavaScript exceptions from `wc_cart_fragments_params`, and no layout shift attributed to the mini-cart. For the requests I keep, a practical target is a sub-100 ms server response on the origin host, verified with Query Monitor or a `Server-Timing` header rather than a lab score alone. I confirm with field data — CrUX or PageSpeed Insights — that LCP, INP and CLS moved in the right direction. Saif Al Jahan documents this workflow on webdev-saif.com because a deleted request is not automatically a faster page.

Trade-offs and the mistakes I see most often

The common failure is hiding the mini-cart with CSS and assuming the request stopped — it did not, and the DOM swap still runs. Another is deregistering fragments globally, which breaks AJAX add-to-cart on every product page. Plugins often re-add their own fragment callbacks, so I re-check the filter after each deployment. There is also a security trade-off: making the fragment endpoint cacheable is tempting, but removing the nonce or caching logged-in responses introduces stale prices and broken sessions. I keep the nonce, scope caching to anonymous visitors, and leave cart and checkout pages untouched. Finally, a cookie-only counter can drift after a background order change, so I reconcile it on navigation rather than trusting it indefinitely.

Frequently asked questions

What are WooCommerce cart fragments?

They are small pieces of HTML that WooCommerce refreshes through an AJAX call after a page loads. The `woocommerce_get_refreshed_fragments` action returns selector-to-HTML pairs for the mini-cart and counters, and `wc-cart-fragments.js` swaps them in. The point is to update the cart without a full reload.

Does disabling cart fragments break AJAX add to cart?

It can, because AJAX add-to-cart relies on the fragment refresh to update the badge and mini-cart. If you dequeue `wc-cart-fragments`, filter `woocommerce_add_to_cart_redirect` to send shoppers to the cart page, or disable AJAX add-to-cart so the button performs a normal POST. Test both logged-in and logged-out sessions before shipping.

Which pages should not load cart fragments?

Catalogue pages without a cart widget, landing pages that send buyers to an external checkout, and block-based carts using the Store API rarely need them. High-traffic routes where the origin is close to its TTFB budget are also good candidates. Keep the refresh on shop archives if customers expect an instant mini-cart update.

Can I cache the cart fragments response?

Partly. You can cache the HTML your own `woocommerce_add_to_cart_fragments` callback produces using `wp_cache_get` and `wp_cache_set` keyed by the cart hash, which avoids repeating expensive queries. Do not cache logged-in fragments or remove the AJAX nonce to make the endpoint cacheable; that risks stale prices and broken sessions.

Do cart blocks still use cart fragments?

Cart and checkout blocks read from the Store API at `wc/store/v1/cart` instead of the classic fragment endpoint. On a block-based storefront, classic `wc-cart-fragments` output is usually redundant and can often be dequeued, provided no theme widget or plugin still consumes the `wc_cart_fragments_refresh` event. Check the rendered source first.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .