Woocommerce cart fragments are the Ajax requests that are responsible for handling the cart option on the header. For example, when the users on your website add a product in the cart and later click on the cart option on the header, this request shows the items available on the cart. For instance, this script enables your cart to display its contents when the user hovers over it in the header.
Why do I need to disable this request?
These requests are enabled by default on fresh installation of the WooCommerce plugin and seems like a great feature to have. The only downside of this feature is that if you are on a slow hosting provider, it might take up to 3-5 minutes to load and thus reduces the overall loading speed of your entire website. We recommend you to disable this feature if you are on a slow hosting provider.
How to disable the cart fragments?
You can use the following steps to disable the cart fragments script on your WooCommerce website:
Step1: Navigate to the WooCommerce Settings and click on products
Step 2: uncheck the box that says โEnable Ajax add to cart button archiveโ
Step 3: Check the box that says โRedirect to the cart page after successful additionโ
These options are technically optional but this will ensure to bypass any cache plugin that you have installed on your website.
Step 4: in order to dequeue the JavaScript you may need to add the following snippet into the functions.php file of your child theme. If you are not sure what a Child Theme is, here is an article that will guide you through. Please navigate to the appearance and theme file editor and click on the child themeโs function.php file.
Copy the snippet below and paste into the bottom of the function.php file. Donโt forget to save the file.
/** Disable Ajax Call from WooCommerce */
function sert_dequeue_woocommerce_cart_fragments() {
wp_dequeue_script('wc-cart-fragments');
}
add_action( 'wp_enqueue_scripts', 'sert_dequeue_woocommerce_cart_fragments', PHP_INT_MAX);
The aforementioned code snippet just dequeues the entire JavaScript file and disables the WooCommerce cart fragments to ensure that your site runs faster even on the slow hosting providers.