WooCommerce: Why & How to Disable Ajax Cart Fragments

If you’re here it’s because your WooCommerce website is slow and you’re wondering why the “/?wc-ajax=get_refreshed_fragments” URL generates delays and server loads (spikes).

Besides, there is too much online literature about WooCommerce Ajax Cart Fragments (including specific plugins and performance plugin options), and you want to learn quickly what they are before understanding if and how you should disable them.

Performance optimization tools like Pingdom and GTMetrix often put the blame on this little WooCommerce functionality. And disabling it carefully can give you a boost in speed, page load and ultimately sales conversion rate.

So here’s all you need to know.

What are WooCommerce Ajax Cart Fragments?

But first, for those of you who don’t know that: what is Ajax?

In web development, Ajax (A.J.A.X. = Asynchronous JavaScript And XML) is a web development technique that, in a nutshell, allows you to run functions without refreshing website pages.

Think about the “Ajax Add to Cart” on the WooCommerce Shop page: you can add products to the Cart (and update the Cart) without forcing a page reload; Ajax runs in the background and communicates with the server “asynchronously”.

Now that this is clear, let’s figure out what WooCommerce Ajax Cart Fragments are and what the URL “yoursite.com/?wc-ajax=get_refreshed_fragments” represents.

Ajax cart update on the WooCommerce shop page when Ajax add to cart is enabled. Cart widget refreshes without forcing a page reload / redirect.

Long story short, even on small sites, and even on non-WooCommerce pages, WooCommerce tries to “get” the shopping cart details so that it can be ready to “recalculate” the Cart every time something is done (or not done!) on a given WordPress page.

This allows WooCommerce to keep the Cart widget updated and to immediately “listen” to any Ajax Add to Cart event that might require a Cart update.

Basically, WooCommerce calls “/?wc-ajax=get_refreshed_fragments” in order to update the Cart items and Cart total asynchronously i.e. without the need of refreshing the website page you’re visiting.

Ajax is awesome and all, however don’t underestimate the performance implications and plugin conflicts this little functionality might cause. Which takes us to the next section…

Why disabling WooCommerce Ajax Cart Fragments?

In order to make the Cart update on every page of your website, WooCommerce runs this Ajax functionality every time.

Even on the About page. Even on the homepage if you have no products. Even on the Contact page if you just have a contact form.

If your theme does not provide a WooCommerce cart drop-down widget, and if you have no products that can be added to Cart on a specific website page, then you’d better remove the whole Ajax functionality.

What’s more, if you choose from the WooCommerce settings to redirect users to the Cart after adding any product to Cart, you’re definitely forcing a page redirect (to the Cart page), so having the Ajax Cart Fragments active is quite pointless.

Under WooCommerce > Settings > Products > General it’s recommended to disable Ajax add to cart behavior and, if possible, to enable redirection to the Cart page. This will always force a page reload (and/or a redirect) and therefore will save the user an Ajax call needed to update the Cart on the go.

Ideally, the only places where “/?wc-ajax=get_refreshed_fragments” should run are pages and WooCommerce archives where your customers can add to cart AND you want to use a dynamic cart widget

For example, if you have Add to Cart buttons on your category pages AND you want the Cart widget to update accordingly without a page reload (and you have Ajax add to cart enabled), then you need /?wc-ajax=get_refreshed_fragments active.

Besides, on the Cart page you can change quantities or remove items without refreshing the page, and in there you also want the Cart widget to update accordingly (but the real question here is: why is there a cart widget on the Cart page as it makes no sense?). So, on the Cart page you also need “/?wc-ajax=get_refreshed_fragments” active, or the widget won’t refresh if you update the Cart.

Conclusion:

  1. if your theme does NOT have a dynamic header Cart widget, you can disable “/?wc-ajax=get_refreshed_fragments”
  2. if your theme has a dynamic header Cart widget, but you don’t care about showing the Cart widget content on the go, you can disable “/?wc-ajax=get_refreshed_fragments” everywhere
  3. if you want to keep the Cart widget functionality active, you should disable “/?wc-ajax=get_refreshed_fragments” ONLY on those pages where there is no Ajax Add to Cart functionality (WooCommerce Product Archives) or Cart update functionality (Cart page)

How to Disable WooCommerce Ajax Cart Fragments?

Now that we understood what Cart Fragments are and why/when they should be removed, we can get into a bit of coding.

Of course, there are plugins for that – but when you can achieve a functionality such as this one with a few lines of PHP it makes no sense to find a different solution, even if you don’t know how to code.

But first, let’s see how WooCommerce adds this Ajax call (in development terms we would say “how it enqueues this script”).

First of all, the script “wc-cart-fragments” is described by a function called “register_scripts()”. It calls a JS script from the /assets folder and requires JQuery and cookies to be enabled:

'wc-cart-fragments' => array(
	'src'     => self::get_asset_url( 'assets/js/frontend/cart-fragments' . $suffix . '.js' ),
	'deps'    => array( 'jquery', 'js-cookie' ),
	'version' => WC_VERSION,
),

In the same file, this is the time “wc-cart-fragments” gets called:

self::enqueue_script( 'wc-cart-fragments' );

If we look at the “enqueue_script()” function we’ll find out that our “wc-cart-fragments” script is first registered and then enqueued as per the WordPress documentation (https://developer.wordpress.org/reference/functions/wp_enqueue_script):

private static function enqueue_script( $handle, $path = '', $deps = array( 'jquery' ), $version = WC_VERSION, $in_footer = true ) {
	if ( ! in_array( $handle, self::$scripts, true ) && $path ) {
		self::register_script( $handle, $path, $deps, $version, $in_footer );
	}
	wp_enqueue_script( $handle );
}

If something is “enqueued”, then it can be “dequeued” (similar to add_action() and remove_action() PHP functions).

You have to make sure to call the “dequeue” function AFTER the “enqueue” one, so that it’s been already added and you can remove it (hence the priority = 11 as “wc-cart-fragments” is enqueued at default priority of 10).

Tl;dr:

/**
 * @snippet       Disable WooCommerce Ajax Cart Fragments Everywhere
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.4
 * @community     https://businessbloomer.com/club/
 */

add_action( 'wp_enqueue_scripts', 'bbloomer_disable_woocommerce_cart_fragments', 11 ); 

function bbloomer_disable_woocommerce_cart_fragments() { 
	wp_dequeue_script( 'wc-cart-fragments' ); 
}

Please note that in case you have a header cart widget, this will break the “dropdown cart”. You’ll still be able to see the number of items and the cart total in the header, but on hover you won’t get the items and cart/checkout buttons.

On Business Bloomer, I completely disabled the Cart widget hence it makes sense to use this function.

In case you want to just optimize your homepage and leave the “wc-cart-fragments” on the other website pages, you can use this snippet instead:

/**
 * @snippet       Disable WooCommerce Ajax Cart Fragments On Static Homepage
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.4
 * @community     https://businessbloomer.com/club/
 */

add_action( 'wp_enqueue_scripts', 'bbloomer_disable_woocommerce_cart_fragments', 11 ); 

function bbloomer_disable_woocommerce_cart_fragments() { 
	if ( is_front_page() ) wp_dequeue_script( 'wc-cart-fragments' ); 
}

Final Thoughts: WooCommerce Ajax Cart Fragments Yes or No?

We’ve seen that disabling Ajax Cart Fragments “could” give you a boost in website speed but also “could” cause some issues, mostly if you want to keep using your drop-down Cart widget.

So, in this section I want to see what others have found out in regard to “/?wc-ajax=get_refreshed_fragments”.

Does it really give you more benefits than disadvantages? Does it really increase your website page speed? Is it worth it to disable Cart Fragments?

Spoiler alert: it depends.

Colm Troy from CommerceGurus deeply tested Ajax Cart Fragments (as well as other bits you should read in his article: https://www.commercegurus.com/guides/speed-up-woocommerce/) and, in his guide, he found out that:

…the “/?wc-ajax=get_refreshed_fragments” request time takes 448ms which is by far our slowest http request at this point.

On some slower servers with large, poorly optimized databases, this request can often take more than 1-2 seconds to execute.

The good news is that this request is non-blocking and executes well after the DOM is loaded so in general it doesn’t hurt our perceived load times (but definitely hurts our fully loaded times and can hurt some things that GPSI worries about like Time to Interactive and First CPU idle).

He also told me recently that:

I’ve come to the conclusion that WooCommerce novices often end up breaking their sites removing fragments as they’ve not fully thought through all the different scenarios of where a cart widget can appear so it’s definitely one to proceed with caution on.

On WooCommerce stores with load spikes and tons of traffic the first thing we do is dequeue cart fragments, remove cart widgets and have customers get redirected to the cart upon adding to cart.

Keeps things nice, simple and fast 🙂

Want to keep the conversation going? Share your feedback, tests and opinion in the comments 🙂

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Remove / Edit “Added to Your Cart” Message
    A client asked me to completely remove the message that appears after you add a product to the cart from the product page. This is simply done by using a PHP snippet, so here’s the quick fix for you!
  • WooCommerce: Automatically Update Cart on Quantity Change
    There is a lot of literature online that solves this UX problem – so in this article let’s see if I can give you a simplified, working, updated version. So, do you hate the “Update Cart” button too? Yes, the one you have to click after you update the quantity of a product in the […]
  • WooCommerce: Add to Cart Quantity Plus & Minus Buttons
    Here’s a quick snippet you can simply copy/paste or a mini-plugin you can install to show a “+” and a “-” on each side of the quantity number input on the WooCommerce single product page and Cart page. The custom code comes with a jQuery script as well, as we need to detect whether the […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

38 thoughts on “WooCommerce: Why & How to Disable Ajax Cart Fragments

  1. Thanks for this clear post on the fragments! Really helpful Rodolfo!
    Ciao
    Marco

  2. I disabled Ajax cart fragments, and now my flyout cart will not work. What I cannot find is how to reverse what I have done. Any suggestions?

    1. Hi Steve, removing the snippet and clearing your cache will do

      1. Rodolfo, I disabled Ajax cart fragments by pushing a button in Tools>Site heath recommendations. I don’t know where to find the snippet to remove it. functions.php perhaps?

        1. Can you not revert the Site Tools thing?

  3. I copied your snippet and the page runs much faster! I have a plugin that creates a side cart that opens on add to cart. This one works, I removed the original cart widget altogether anyway. Perfect!

  4. Hi – thanks for an informative article. The code doesn’t work for me – the cart fragments took even longer to load and worsened my page performance. Astra theme.

    Thanks
    Karen

    1. Find that weird, I use it on this same website. Have you tried with a different theme and no other plugins than Woo?

  5. Hi, awesome and very simple to understand article. I have one doubt. I have disabled the ajax add to car and I really don’t care that it refreshes the page, but my concern is that it then sends me to the top of the page. Is there a way to remain on the exact same spot one was when clicking on the add to car button?

    1. Hi Renato, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Be advised that directing users directly to cart is not in line with Google’s leaked E-Commerce Playbook. I also suggest A-B-testing the removal of any AJAX cart updates – keeping the AJAX cart can potentially actually reduce a full page load (if the customer goes to checkout directly, for example), so I’d be careful about this (apart from the fact that it may break other functionality).

    1. Disagree on the Google part, fully agree on the other point

  7. Hi there, thanks a lot for that awesome content! Maybe you can help me with a question that relates to this: Is there any chance, that this messes with credit card payments?

    I understood that the sense Ajax Cart Fragments is only cart-related. Yesterday, we encountered a failed credit card payment, which is the first time that happened and also the first one after I disabled Cart Fragments. I tested card payment with two different cards myself and everything was fine. Though, I suspect that if customers are directed to a 3D Secure page to verify their credit cards, there is something wrong with the redirect.

    Could “/?wc-ajax=get_refreshed_fragments” possibly interfere with that or this something entirely different?

    Thanks a lot!
    Oliver

    1. I honestly don’t believe so. The 3D secure page is stored elsewhere so I don’t think this is an issue. Simply disable my code and test again, maybe you encounter the error again

  8. Hi Rodolfo,

    Thank you for this very helpful article, I want to remove WC Ajax Cart Fragments and see how loading times will be reduced.

    Just wanted to ask if the code example needed to be implemented to functions.php of child theme, but you already answered this to Bill´s comment. So i will try it.

    1. Cool!

  9. Hi Rodolfo, I’m using the option: user clicks on add to cart -> go to cart page. Since I have a widget that displays the mini cart with the number of products, if I disable the cart fragments will it be updated after I click on add to cart? (because the page will be updated anyway) Thanks.

    1. Yes it should, but test it first just in case

  10. Hi Rodolfo. I have an online store for groceries and I like the Ajax because I don’t want the whole page to reload each time a product is added to the cut. I however don’t like the fact that each time a product is added to the cart, the mini cart then slides out or drops down to that effect. This blocks other products until the mini cart clears. This means the customer will need to wait until the mini cart clears for them to add more products. How do I disable the dropdown or slide of the mini cart each time a product is added to the cart. Thanks.

    You can check out my website, add a product to the cart and see what I mean.

    1. Hi Brendon that’s not default WooCommerce, must be your theme or a plugin

  11. Hi Rodolfo (ciao sono italiano)
    your trick is ok. I’m using a Yith plugin that manage the add to cart button , so I wanna to get rid of the woocommerce cart . Could you help me?
    Thank you

    1. Hello Gian Paolo, not sure, please ask YITH support

  12. Rodolfo, This is the second or third time that I’ve landed on your site and got a superb, on-the-point snippet that just worked! I just tweaked it to add blog posts to the pages where this needs to be dequeued, but otherwise, just worked. Thanks a bunch!

    Srikanth

    1. Great!

  13. Worked a treat thanks. reduced over a second off my load times.
    I managed to get a 1 – 1.5 second page load time – with 20 active plugins. WPX as the web host which is amazing. (I don’t work for them BTW lol.) but it also took a full day of speed tweaking as much as I know how from googling.

    1. Cool!

  14. This code does not work with Storefront partially.
    When you click on Add to cart and check the minicart it is not working.
    When you reload page, then it is working

    1. Yes, it wouldn’t work with any theme that uses a minicart. In order for the fragments to be disabled, you also need to remove the minicart. A static cart icon that redirects to the Cart page would do

  15. I am a non techy, so please excuse if I have already missed the point. What about some script that just stops AJAX calls on all pages except the WooCo ones (Product, Basket and Checkout)? I am using Astra theme. I guess I just add your script to the end of “functions.php”? Of course I do a back up first. Happy to give a donation if it works. Ajax calls are costing me about 1.7 secs on the home page. Note – Fully Loaded Time has just shot up to 13 secs and is another subject I have yet to investigate.

    1. Yes Bill, in functions.php. Let me know

  16. Didn’t work for me either, I guess it depends on your theme etc. However, this works fine for me for disabling ajax refresh of cart on qty change etc;

    function cart_script_disabled(){
    	wp_dequeue_script( 'wc-cart' );
    }
    add_action( 'wp_enqueue_scripts', 'cart_script_disabled' );
    
    1. Thanks!

  17. Hi Rodolfo, I am creating a book catalog site using Woocommerce to handle all the books and authors registers.
    It’s not an online store, just Woocommerce with catalog mode enabled.
    Your snippet helped me to make site faster… 5s faster!

    Thanks for sharing you knowledge!

    1. Wow!

  18. The code example doesn’t work totally. Ajax cart fragments will still be loaded.

    1. Jakke, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

Leave a Reply

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