Is Woocommerce Slowing Down Your Site? Let’s Fix It.

woocommerce-slow-ajax-cart-fragments

Woocommerce is great, however we can increase significant page loading time to your entire site. If we keep Woocommerce from loading on every page. To accomplish this we just need to add some additional code into our functions.php file of our theme.

So inside your active theme or child theme. Add the following code to your functions.php file. Just as an example the location of your functions.php might be something like: /public_html/wp-content/themes/twentysixteen

Here’s the code:

add_action( 'wp_enqueue_scripts', 'fix_woocommerce_styles_scripts', 99 );

function fix_woocommerce_styles_scripts() {
    //remove generator meta tag
    remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );

    //first check that woo exists to prevent fatal errors
    if ( function_exists( 'is_woocommerce' ) ) {
        //dequeue scripts and styles
        if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
            wp_dequeue_style( 'woocommerce_frontend_styles' );
            wp_dequeue_style( 'woocommerce_fancybox_styles' );
            wp_dequeue_style( 'woocommerce_chosen_styles' );
            wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
            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' );
        }
    }
 }

Woocommerce Cart Fragments

Cart fragments from Woocommerce can slow down a site adding seconds to a page load. This is usually discovered by finding a request to admin-ajax.php in your browser’s developer tools or using GTMetrix.com

There used to be a plugin available title “Disable Cart Fragments”, however the plugin has been removed. Additionally, I’ve found that some plugins and themes will keep loading it, which will cause the traditional fix found here from not working. As reported here on Github: A way to prevent unnecessary, a number of recent users have been unable to remove the cart fragments.

That other code that you might have seen was attempting to remove the script using the wp_enqueue_scripts hook.

/** Disable Ajax Call from WooCommerce on front page and posts*/
add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_cart_fragments', 11);
function dequeue_woocommerce_cart_fragments() {
if (is_front_page() || is_single() ) wp_dequeue_script('wc-cart-fragments');
}

The issue here is when that hook is triggered and if another theme or plugin is loading it back in, you won’t be able to remove it. So if you have a stubborn theme or collection of plugins you can use the following code to remove the Woocommerce cart fragments and shave seconds from your page loads.

/** Disable Ajax Call from WooCommerce on front page, posts, categories, and tags*/ 
add_action( 'shutdown', 'dequeue_woocommerce_cart_fragments');
function dequeue_woocommerce_cart_fragments() {
if (is_front_page() || is_single() || is_category() || is_tag() ) wp_dequeue_script('wc-cart-fragments');
}

We’re using the shutdown hook here as a last ditch effort to remove the script. The reason this works is that the shutdown hook fires right before all the PHP finishes execution. This will remove the cart fragments from the front page, posts, categories, and tags. One thing to note is that disabling the cart fragments will break your site’s cache busting ability to update information about the user’s cart. So if you have a cart icon with a number or other element next to it, that element will likely not work.

If you have any questions or are still getting stuck with this, post them in the comments below or reach out to us to schedule a time to take a look at your site.

4 thoughts on “Is Woocommerce Slowing Down Your Site? Let’s Fix It.”

    1. Yes, it is, it deque’s the script far later which fixes most of the issues of other plugins and themes enqueuing the script after the plugin is loaded. It also gives you the control of which page types you want to keep it from loading on. This is important because not loading this script can break parts of your site that depend on the cart fragment. But if it’s the homepage of your business site or a blog post, you’ll almost never need to load all of Woocommerce or the cart fragments. The above code fixes that.

Leave a Reply