
Optimizing WooCommerce CPU Usage
Boost WooCommerce speed by disabling Cart Fragments.
How to Disable WooCommerce Cart Fragments for Better Performance
WooCommerce is a fantastic platform for building online stores, but sometimes, default features designed for convenience can unintentionally slow down your website. One common culprit is the “Cart Fragments” AJAX request. While it serves a purpose, it often runs unnecessarily and can significantly impact your site’s loading speed. Let’s dive into what it is, why it can be problematic, and how you can disable it for a faster store.
What is the WooCommerce Cart Fragments AJAX Call?
Have you noticed how on many WooCommerce stores, the mini-cart icon in the header updates automatically when you add a product, without needing a full page refresh? That’s the Cart Fragments feature in action.
Technically, WooCommerce uses an AJAX (Asynchronous JavaScript and XML) call, specifically ?wc-ajax=get_refreshed_fragments
, to fetch the latest cart contents (like the item count and subtotal) and update the mini-cart display dynamically.
Why Can Cart Fragments Slow Down Your Site?
While dynamic updates sound great, the implementation of Cart Fragments has a few significant performance drawbacks:
It runs on (almost) every page: This AJAX request is typically triggered on every single page load across your WordPress site, not just shop-related pages. Even your blog posts, contact page, or homepage might be making this call in the background.
It bypasses caching: Because the cart content needs to be fresh, this AJAX request is often configured not to be cached by standard page caching systems (like those used by hosting providers or caching plugins). This means it hits your server directly every time.
It adds server load: Each uncached request consumes server resources (CPU, memory). On high-traffic sites, or even moderately busy ones, these frequent calls can add up, slowing down response times for all users.
It impacts Core Web Vitals: A slow, uncached AJAX call firing on page load can negatively affect metrics like Time To First Byte (TTFB) and potentially Largest Contentful Paint (LCP), which are crucial for user experience and SEO.
How to Check if Cart Fragments is Impacting You
You can easily check the impact of this request using your browser’s developer tools:
- Right-click anywhere on your site and select “Inspect” or “Inspect Element”.
- Go to the “Network” tab.
- Reload the page (you might need to do a hard refresh - Ctrl+Shift+R or Cmd+Shift+R).
- Look for a request containing
?wc-ajax=get_refreshed_fragments
. - Check its loading time in the “Time” or “Waterfall” column. If it’s taking several hundred milliseconds or more, it’s likely contributing to slower page loads. Check this on various page types (homepage, product page, blog post).
The Solution: Disabling Cart Fragments
💡 Easiest Method: Use a Plugin For the simplest approach, you can use the Disable Cart Fragments plugin by Optimocha. This lightweight plugin effectively disables cart fragments while the cart is empty, but enables it when needed. Simply install, activate, and you’re done - no coding required!
If you prefer not to use a plugin, you can accomplish the same result by adding a small PHP code snippet to your site. This gives you more control and reduces the number of plugins on your site.
💡 Where to Add Code: The safest way to add PHP snippets is using a dedicated plugin like Code Snippets or by adding it to your child theme’s
functions.php
file. Avoid editing your main theme’s files directly, as changes will be lost upon theme updates.
Here’s the code snippet to disable the Cart Fragments AJAX call:
<?php
add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments_script', 11 );
function dequeue_woocommerce_cart_fragments_script() {
wp_dequeue_script( 'wc-cart-fragments' );
}
?>
Understanding the Trade-off
⚠️ Important Consequence: Disabling this script means your mini-cart (often in the header) will not update immediately when a customer adds an item to the cart using AJAX buttons on the same page. The cart will update correctly once the customer navigates to a new page, including the main Cart page or Checkout page. The core cart functionality remains unaffected.
For many stores, this trade-off is acceptable. Users typically proceed to the cart or checkout page after adding items, where they’ll see the updated information. The performance gain across the entire site often outweighs the slight change in mini-cart behaviour.
Should You Disable Cart Fragments?
Consider disabling Cart Fragments if:
- Your website performance is suffering, and tests show the
get_refreshed_fragments
call is slow. - Instant mini-cart updates are not critical to your user experience.
- You prioritize overall site speed and reduced server load.
- Your theme perhaps offers alternative ways to display cart counts or doesn’t rely heavily on the dynamic mini-cart.
Test Before and After!
Whenever you make changes like this, it’s crucial to test:
Speed Tests: Run speed tests (using tools like GTmetrix, PageSpeed Insights) before and after adding the code snippet to measure the performance improvement. Pay attention to TTFB and overall load times.
Functionality Tests: Thoroughly test your add-to-cart process. Ensure items are added correctly, and the main cart and checkout pages display the right information.
Conclusion
The WooCommerce Cart Fragments AJAX call is a well-intentioned feature that can unfortunately become a performance bottleneck. By understanding its purpose and impact, you can make an informed decision about whether disabling it is right for your store. Adding a simple code snippet can often lead to significant speed improvements across your entire site, improving user experience and potentially boosting your SEO rankings. Just remember to test carefully after implementing the change!
Excellent advice!