console.dir({"ID":193,"post_author":"1","post_date":"2019-02-11 14:10:06","post_date_gmt":"2019-02-11 22:10:06","post_content":"\n

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. <\/p>\n\n\n\n

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<\/p>\n\n\n\n

Here's the code:

<\/p>\n\n\n\n

add_action( 'wp_enqueue_scripts', 'fix_woocommerce_styles_scripts', 99 );\n\nfunction fix_woocommerce_styles_scripts() {\n    \/\/remove generator meta tag\n    remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );\n\n    \/\/first check that woo exists to prevent fatal errors\n    if ( function_exists( 'is_woocommerce' ) ) {\n        \/\/dequeue scripts and styles\n        if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {\n            wp_dequeue_style( 'woocommerce_frontend_styles' );\n            wp_dequeue_style( 'woocommerce_fancybox_styles' );\n            wp_dequeue_style( 'woocommerce_chosen_styles' );\n            wp_dequeue_style( 'woocommerce_prettyPhoto_css' );\n            wp_dequeue_script( 'wc_price_slider' );\n            wp_dequeue_script( 'wc-single-product' );\n            wp_dequeue_script( 'wc-add-to-cart' );\n            wp_dequeue_script( 'wc-cart-fragments' );\n            wp_dequeue_script( 'wc-checkout' );\n            wp_dequeue_script( 'wc-add-to-cart-variation' );\n            wp_dequeue_script( 'wc-single-product' );\n            wp_dequeue_script( 'wc-cart' );\n            wp_dequeue_script( 'wc-chosen' );\n            wp_dequeue_script( 'woocommerce' );\n            wp_dequeue_script( 'prettyPhoto' );\n            wp_dequeue_script( 'prettyPhoto-init' );\n            wp_dequeue_script( 'jquery-blockui' );\n            wp_dequeue_script( 'jquery-placeholder' );\n            wp_dequeue_script( 'fancybox' );\n            wp_dequeue_script( 'jqueryui' );\n        }\n    }\n }<\/code><\/pre>\n\n\n\n

Woocommerce Cart Fragments<\/h2>\n\n\n\n

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 <\/a><\/p>\n\n\n\n

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 <\/a>from not working. As reported here on Github: A way to prevent unnecessary<\/a>, a number of recent users have been unable to remove the cart fragments. <\/p>\n\n\n\n

That other code that you might have seen was attempting to remove the script using the wp_enqueue_scripts hook. <\/p>\n\n\n\n

\/** 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');
}<\/pre>\n\n\n\n

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. <\/p>\n\n\n\n

<\/p>\n\n\n\n

\/** 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');
}<\/pre>\n\n\n\n

We're using the shutdown hook<\/a> 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. <\/p>\n\n\n\n

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. <\/p>\n","post_title":"Is Woocommerce Slowing Down Your Site? Let's Fix It.","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"closed","post_password":"","post_name":"is-woocommerce-slowing-down-your-site-lets-fix-it","to_ping":"","pinged":"","post_modified":"2019-02-11 14:27:24","post_modified_gmt":"2019-02-11 22:27:24","post_content_filtered":"","post_parent":0,"guid":"https:\/\/www.eckmandesign.com\/blog\/?p=193","menu_order":0,"post_type":"post","post_mime_type":"","comment_count":"4","filter":"raw"});lang="en-US">

Is Woocommerce Slowing Down Your Site? Let's Fix It. - Eckman Design

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.