How To Disable WooCommerce Cart Fragments Ajax
Completely Disabling WooCommerce Cart Fragments AJAX becomes essential because it slows down your website speed by making continuous requests By WordPress admin-ajax.php. Request in WooCommerce or cart fragments affect your WooCommerce page loading speed up to 15 seconds, affecting your eCommerce website revenue Direct. And in my last article about Tool To Test Website Speed Page Speed And Performance, Clearly mentioned page speed affects your website SEO and ranking in google. Even Matt Cutt Said the same things.
Now let’s come to the main points. Due to a high number of AJAX requests ( wc-ajax=get_refreshed_fragments) on your site. AJAX requests are usually uncached and take a few seconds to load, leading to an increase in your page load time. To solve this issue, you have to disable WooCommerce cart fragments AJAX. Let’s see what it is and how it works.
- What are WooCommerce Cart Fragments?
- Without Plugin Method
- Replacing your WooCommerce button
- Using Plugins
- Page Speed Test
- Conclusion
What are WooCommerce Cart Fragments?
Disable WooCommerce Cart Fragments wc-ajax=get_refreshed_fragments , is a script using admin AJAX. The script tries to collect your cart information by calling the script and hence updates your cart without refreshing the page. It tries to gather the uncached cart details, slowing down your site speed and consuming high server resources. The major problem is that it also works on pages that contain no cart details or product-related information. The AJAX call generally overrides the caching plugin you do for optimizing your eCommerce. It is suitable for your site in terms of user interface but, on the other hand, consumes a lot of time in page loading.
For example, cart fragments on static posts or feed pages like the About Us page, contact us page, blogs page that doesn’t require cart information slows down your site’s speed and leads to poor site performance. Therefore, instead of removing the cart button from these pages, we have to disable cart fragments or disable cart updates.
To identify the cart fragments on your site, you need to run a page speed test like Google Page Speed Insights Tool or GTMetrix. So to Troubleshoot the cart fragments issue, we have separated the blog into two methods. One method uses plugins, and the second method is done by programmatically coding without plugins.
How To Disable Cart Fragments in WooCommerce Without Plugin.
To disable the cart fragments in WooCommerce, you have to paste the below-mentioned code in the theme’s functions.php file by visiting your WordPress admin panel. So the clear path will be wp dashboard >> Appearance >> theme editor >> function.php page.
There are three ways to disable the WooCommerce Ajax cart fragments without plugins.
- Disabling cart fragments from all page
- Disabling on the front page
- Disabling on front page and posts
- Disabling on all pages except the shop page.
1. Disabling cart fragments from all page
You need to paste the below-mentioned code in the fnctions.php file.
/** Disable Ajax Call from WooCommerce */
add_action( ‘wp_enqueue_scripts’, ‘gomaya_dequeue_woocommerce_cart_fragments’, 11);
/** Disable Ajax Call from WooCommerce on entire website*/
add_action( 'wp_enqueue_scripts', 'gomaya_dequeue_woocommerce_cart_fragments', 11);
function gomaya_dequeue_woocommerce_cart_fragments() { wp_dequeue_script( 'wc-cart-fragments' ); return true; }
Note – You can simply replace the real cart with the text “cart” or with a normal image and add cart link to avoid loadig issue it
2. Disabling cart fragments on the front page
Go to your WordPress dashboard to disable the WooCommerce AJAX cart fragments on the front page. After that, navigate to Appearance and click on Editor. Here, you have to add a code to disable the Ajax Call from WooCommerce at the end of this file. After adding the code to this file, click on the Update button to save your changes.
/** Disable Ajax Call from WooCommerce front page */
add_action( 'wp_enqueue_scripts', 'gomaya_dequeue_woocommerce_cart_fragments', 11);
function gomaya_dequeue_woocommerce_cart_fragments() { if (is_front_page()) wp_dequeue_script('wc-cart-fragments'); }
You can also use File Transfer Protocol (FTP) to disable the WooCommerce Ajax Cart Fragments script on your site’s front page. To begin with, log in to your FTP account and navigate to wp-content. Inside wp-content, you have to find your theme and locate the functions.php file.
Here, you have to add the code to disable the Ajax Call from WooCommerce at the end of this file. After that, upload the updated file to your server.
After updating the file to the server, go to WooCommerce and click on the settings tab. Then, go to the products tab and click on the “Display” section. Here, you will find a section named “Add to Cart Behavior” You have to enable the checkbox against the option “Redirect to the cart page after successful addition “under this section.
3. Disabling cart fragment on front page and posts
To disable WooCommerce cart fragments on your front page, you must follow the above steps.
If you want to disable the WooCommerce Cart Fragments script on your posts, you have to add the following code at the end of your theme’s function.php file.
/**
* @snippet Disable Ajax Call from WooCommerce on front page and posts - WooCommerce
* @author Gomahamaya.com
* @donate $5 https://www.paypal.com/paypalme/gomahamaya
*/
add_action( 'wp_enqueue_scripts', 'gomaya_dequeue_woocommerce_cart_fragments', 11);
function gomaya_dequeue_woocommerce_cart_fragments() {
if (is_front_page() || is_single() ) wp_dequeue_script('wc-cart-fragments');
}
4. Disabling cart fragments on all pages except the shop pages
WooCommerce consists of several stylesheets and scripts which use your server resources to load, thus, slowing down your site speed. You can disable the cart fragments scripts on all your blog posts to save the usage of your server’s resources and enhance your site’s load time. You can apply the WooCommerce related scripts only on your shop pages. This will help your site load faster and give your users a seamless experience on your site.
/** Disable All WooCommerce Styles and Scripts Except Shop Pages*/
add_action( 'wp_enqueue_scripts', 'gomaya_dequeue_woocommerce_styles_scripts', 99 );
function gomaya_dequeue_woocommerce_styles_scripts() { if ( function_exists( 'is_woocommerce' ) ) { if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) { # Styles wp_dequeue_style( 'woocommerce-general' ); wp_dequeue_style( 'woocommerce-layout' );
wp_dequeue_style( 'woocommerce-smallscreen' );
wp_dequeue_style( 'woocommerce_frontend_styles' );
wp_dequeue_style( 'woocommerce_fancybox_styles' );
wp_dequeue_style( 'woocommerce_chosen_styles' );
wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
# Scripts
wp_dequeue_script( 'wc_price_slider' );
wp_dequeue_script( 'wc-single-product' );
wp_dequeue_script( 'wc-add-to-cart' );
wp_dequeue_script( 'wc-cart-fragments' );
wp_dequeue_script( 'wc-checkout' );
wp_dequeue_script( 'wc-add-to-cart-variation' );
wp_dequeue_script( 'wc-single-product' );
wp_dequeue_script( 'wc-cart' );
wp_dequeue_script( 'wc-chosen' );
wp_dequeue_script( 'woocommerce' );
wp_dequeue_script( 'prettyPhoto' );
wp_dequeue_script( 'prettyPhoto-init' );
wp_dequeue_script( 'jquery-blockui' );
wp_dequeue_script( 'jquery-placeholder' );
wp_dequeue_script( 'fancybox' );
wp_dequeue_script( 'jqueryui' ); } }
You have to apply any of these three code blocks to disable WooCommerce cart fragments scripts, i.e., wc-ajax=get_refreshed_fragments, from all pages and posts except the shopping pages on your WooCommerce site.
Replacing your WooCommerce button
There’s another way to solve this issue and fasten your site’s load speed. You can replace your WooCommerce button in the header with any button which links back to your WooCommerce cart page. Here, the users have to switch to another page to access their cart. However, this step removes the AJAX call from your site and reduces your site load time by a few seconds.
5. Disabling Cart Fragments Using Plugins
If you don’t want to play around with the codes and modify your theme’s file, you can use these simple plugins to disable the AJAX calls on your WooCommerce site.
1. WP Rocket
It is one of the best WordPress caching plugins that helps you enhance the speed of your website, and you don’t need to use any codes as well. Setting up and using this plugin is no rocket science. All you need to do is install and activate the plugin, and after that, it will do for you. Once you activate the plugin on your WooCommerce site, it will automatically detect and disable the WooCopmmerce caret fragments on all pages of your site.
The plugin detects cart fragments in the source code of your pages and checks if the cart is empty while loading the page. If both conditions are fulfilled, WP Rocket caches the page and disables the AJAX calls on your page.
Note: You have to purchase the premium version of the WP Rocket plugin to serve the purpose. You can also use the helper plugin in case you encounter some problems.
2. Disable Cart Fragments by Optimocha
Disable Cart Fragments by Optimocha is an easy-to-use plugin that disables all the WooCommerce cart fragments scripts and helps your website load faster. The plugin works only when the cart is empty. When products are in the cart, the plugin also enqueues the script back. Hence, this plugin works the best in both situations, i.,e., it helps you cache your site and shows the cart totals as and when a user adds the product to the cart. The best part about this plugin is that you don’t need to play arou7nt the settings. You have to install and activate the plugin, and then it will automatically do the task for you.
3. Perfmatters
Perfmatters plugin helps you to Disable (wc-ajax=get_refreshed_fragments) WooCommerce cart fragments AJAX. To disable WooCommerce carts fragments using Perfamatters, ensure that you’ve WooCommerce installed and activated.
After that, go to the Perfamatters plugin and click on the settings tab.
Then, navigate to the Options tab and select the General menu.
Then you have to scroll down and find the WooCommerce section. Inside the WooCommerce section, toggle the “Disable Cart Fragmentation.” key and click on the “Save Changes” button. This will solve your issue and help your website load faster.
Page Speed Test
After adding the codes to your the3mes’ function.php file, you have to clear the cache of the caching plugin. After disabling the cart fragments, check the website load speed by using the speed tool. Now, we can fix the wc-ajax=get_refreshed_fragments script loading issue under the render-blocking JavaScript section on Google PageSpeed Insights.
Conclusion
WooCommerce helps you build an online store in WordPress. However, it consists of several stylesheets and scripts, which slows down your website. In such a case, you can disable WooCommerce cart fragments scripts on all the pages and posts except the shop pages to improve your website’s speed.
You may also be interested in Landing page plugins, Email marketing services, membership plugins other WooCommerce Plugins.