How to Disable WooCommerce Cart Fragments Without a Plugin

If your a WordPress developer or run your own ecommerce store you probably noticed one thing on GTmetrix or Google PageSpeed ​​Insights wc ajax get refreshed fragments this little script can ruin the loading speed of your entire website. In this article we all learn Stop WooCommerce Cart Fragments Without Installing a Plugin to make your checkout experience super fast and boost sales.

Basically whenever a user visits your site WooCommerce sends an AJAX request to update the cart items on every page load. Even if the user cart is empty this request continues to run in the background. This increases the load on the server and causes a very high TTFB . If you want to optimize your site without any unnecessary plugins you come to the right place. We all shut it down with the help of code.

Understanding WooCommerce Cart Fragments:

Look brother in simple words WooCommerce cart fragments is a feature that keeps your store mini cart updated dynamically on every page. Suppose a user adds a product to the cart then without refreshing the page the cart icon in the top menu starts showing  item. This looks great but the story behind it is quite painful.

To enable this feature WooCommerce wcmajax=get refreshed fragmentsmakes a call called cart. This AJAX call boots the core of WordPress on every single page load. This simply means that if 100 people are browsing your site simultaneously your server will check the entire database and plugins 100 times regardless of whether those people have added something to their cart or not.

This issue is technically known as Admin Ajax Bloat . When your server starts processing every request like non cached pages your caching plugin also fails. Static pages also start loading slowly which is a major red flag for both your user experience and SEO.

Why Plugins May Not Be the Best Solution

A number of plugins make it easy to disable this feature with one click. But a professional developer or serious store owner will never want to install a new plugin for such a small task.

Each additional plugin adds new rows to your database increasing the risk of security vulnerabilities and loading its own additional scripts. Why depend on a third plugin when we can do the same thing ourselves by writing just 4-5 lines of clean code?

Apart from this many optimization tools like Perfmatters of WP Rocket There a built in option to disable this but if your not using a premium optimization tool the best and cleanest way is to install custom code in the functions.php file. This gives you complete control and do not add any extra weight to your site.

Disable Cart Fragments in WooCommerce Using Code

The next thing we need to do is take action. We all block this AJAX call using login code but in a way that allows your cart page to function normally. Some people delete the entire script which causes problems during checkout but we all use the safest method.

functions.phpFirst of all to be on the safe side  back up your active theme file or use a child theme so that your code is not deleted in future updates.

 Open Your Theme functions.php File

You need to go to your WordPress dashboard there click on Appearance  Theme File Editorfunctions.php You will see the file on the right side click on it to open it in the editor if you are using the Control Panel go to the File Manager and wp content themes your theme functions.phpedit it.

Paste the Optimization Code

Paste the code below at the very end of the file this code carefully checks if the user is on a normal page  then it will dequeue  the fragment script but will allow it to run on the Cart and Checkout pages so that functionality is not impaired.

PHP

 Redirect Users to Cart Page After Adding a Product

Most importantly when you disable fragments the mini cart wont automatically update after adding a product. A simple way to handle this is to redirect shoppers to the cart page after they click the Add to Cart button.

You dont need to write any code for this simply go to woocommerce  settings  products in your WordPress admin there under the general tab in the Add to cart behavior section tick the Redirect to the cart page after successful addition checkbox and save the settings.

Otherwise if you dont want to redirect you can make your dynamic header menu icon a normal static link that points directly to the URL. This will allow the user to click and view their cart and your site will load like a bullet without any AJAX load.

Advanced Performance Tweak:

Actually when you disable cart fragments using the code above there a small problem. When a user is browsing other pages of the website the cart counter in the header shows  or is completely empty even if they added items to the cart. There a solid alternative method that will maintain 10x the speed and still show the user the correct item count.

We can save the cart count directly in the user browser using JavaScript sessionStorage. This will eliminate any server calls and keep your page speed score green. Lets see what advanced code to add to functions.php

The biggest advantage of this code is that it does not disturb your cache-friendly pages. WordPress Developer Docs According to eb ?

title Inline loading scripts reduces request overhead of external files which is critical to improving core web vitals loading inline scripts reduces the request overhead of external files which is crucial to improving core web vitals.

 Code vs Plugin vs Default WooCommerce

Now lets get a clearer picture of what this optimization ultimately does to your site. Many people wonder what wrong with leaving the default setup in place so this custom table will explain the whole story.

Performance Metric Default WooCommerce Setup Using Popular Optimization Plugin Manual Code Snippet Method
Average TTFB Delay 400ms 1.2  100ms  200ms 0ms 
Database Queries 20  extra queries per page 2-5 helper queries 0 Extra Queries
Server Resource Usage High CPU & Memory Spike Medium  Extremely Low & Lean
Risk of Code Conflicts None Medium  Low 
File Size Added 45KB JavaScript executed Varies  0KB 

 Pros and Cons of Disabling Cart Fragments

Look no technological optimization is 100% perfect. If something has benefits some compromises are bound to be made. So without any filter lets understand both its pros and cons:

The Pros 

  • Server Loading Reduction: Your shared VPS hosting CPU usage will drop because the PHP processor and MariaDB database will not be loaded repeatedly for each user.

  • Flawless Caching Integration: If your using Cloudflare WP Rocket or LiteSpeed all your pages will show 100% cache hits.

  • Better SEO Rankings: The LCP  and TTFB scores will turn green in Google PageSpeed ​​Insights which has a direct benefit in rankings.

  • Zero Plugin Bloat: You dont have to depend on code from third party developers making the risk of site hacking almost nil.

The Cons

  • No Real Time Mini Cart Sidebar: If your site has a slide out cart menu that opens by clicking on every page it will not show new items without a refresh.

  • Manual Setup Required: If you ever change the theme you will have to re-insert this code into the functions.php file of the new theme.

Clean FAQs

Does disabling cart fragments ruin the checkout page?

Absolutely not. The code we used above  is cart && is checkoutchecks for this. This means that whenever a user is on the actual cart or checkout page WooCommerce default scripts will run as normal and they wont have any problems completing the transaction.

Do I need to create a child theme for this code?

It not necessary but it a best practice. If you paste the code directly into the parent theme your custom code will be deleted whenever a new theme update is released. To avoid this use a child theme or use a utility plugin called Code Snippets.

I uploaded the code but GTmetrix is ​​still showing wc jax. Why is that?

The most common cause is an outdated cache. After installing the code click the Clear Cache button on your WordPress caching plugin If you using Cloudflare do a Purge Everything there as well then check in incognito mode.

Wouldnt enabling redirects on my site ruin the user experience?

It really depends on user behavior. If your store sells limited items a direct cart redirect can increase conversion rates. However if your running a larger store like grocery or fashion where people buy 10 items at once, you might want to consider a dynamic AJAX cart instead of a redirect.

Do premium themes like Astra or Kadence have this option built in?

Yes todays modern speed optimized themes like Astra Pro or GeneratePress have WooCommerce performance settings directly within the customizer where you can toggle it. If your using such a theme you dont even need to enter any code.

Helping Blogs

How to Get Rid of Lock Icon on Windows 11 Hidden Folders

How to Transfer a Domain from GoDaddy to Hostinger Fast

Final Thoughts

So the point is every millisecond counts in an e-commerce business. If your site takes more than 3 seconds to load customers will simply abandon it and move on to another more powerful site. This method ofTurn Off WooCommerce Cart Fragments Without a Plugin is the best organic formula for making your store fast and rock solid.

So without further ado go to your WordPress dashboard open the functions.php file and apply this small optimization snippet to make your website loading speed rocket!

Leave a Comment

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

Scroll to Top