Published: July 17, 2024 by Lucas Rolff
Optimizing WooCommerce by getting rid of wc-ajax=get_refreshed_fragments
WooCommerce quite often get the shopping cart information via the AJAX call ?wc-ajax=get_refreshed_fragments
. This works perfect if you're using a cache plugin.
However, the major problem with the above call is the fact that it's even performed when there's no products in the cart - we're then wasting CPU resources that could be used for serving actual visitors, and thus allowing the site to scale even better.
Luckily Optimocha made an amazing plugin in WordPress to solve this problem: Disable Cart Fragments by Optimocha.
What is does is rather simply, it disables the cart fragments by default, and then check whether the cart cookies are set and not empty - if they're not empty it will then load the cart fragments script and trigger the AJAX call.
With this, we only trigger the AJAX call whenever people actually add anything to the shopping cart, which often is a far lower percentage than those who actually visit the site.
We happened to have a customer that had high traffic on their WooCommerce site, as an initial measure to allow traffic to increase, we temporarily allocated more resources to the account (explains the huge spikes in the below graph), and later the Cart Fragments plugin got installed: On the page, the Disable Cart Fragments got enabled at 18:30 UTC (6.30pm), and the LiteSpeed Cache got flushed as a part of this, which you see a few bigger spikes as well shortly after as the pages would have to be recached.
However, only 4 minutes later, the CPU had dropped to an average of 50% (half a CPU core) compared to the roughly 150-200% (1.5-2 CPU cores) prior to the plugin being enabled - the customer in this case saved more than 1 full CPU core by using a simple plugin to check for a cookie and thus enable the WooCommerce Cart Fragments, only when they were actually needed.