Exclude Slow Woocommerce Scripts From Non-Store Pages In WordPress

Categorized as Speed
Woocommerce Featured Image

Time

>20 minutes

Difficulty

3/10

Impact

4/10

Speed Up Woocommerce

Woocommerce is a staple of many WordPress websites that utilize a store, checkout process or sell something directly to visitors on their WordPress site. Unfortunately, it’s added by default to many themes and requires that sites load bulky Woocommerce code on every page, even when it isn’t needed or used.

This can be fixed for most sites by removing these scripts from pages that aren’t the “cart” or “store” pages that Woocommerce is actually needed on.


Stripping out the slow, unneeded Woocommerce code

Most sites don’t need all of Woocommerce loaded on every single page. While some sites may have more complex e-commerce setups, most publishers simply can remove the unnecessary bulk by adding the code below to a child theme of their Woocomerce site.

Step 1: Create a child theme or your existing theme if you haven’t already

Step 2: Add the code below to your functions.php file

This can be done by going to Appearance > Theme Editor > [select the child theme] > functions.php — then add the code to that file by copy pasting below

/**
 * Disable WooCommerce block styles (front-end).
 */
function slug_disable_woocommerce_block_styles() {
  wp_dequeue_style( 'wc-block-style' );
}
add_action( 'wp_enqueue_scripts', 'slug_disable_woocommerce_block_styles' );
/**
 * Manage WooCommerce styles and scripts.
 */
function grd_woocommerce_script_cleaner() {
	
	// Remove the generator tag
	remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) );

	// Unless we're in the store, remove all the cruft!
	if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
		wp_dequeue_style( 'woocommerce_frontend_styles' );
		wp_dequeue_style( 'wc-block-vendors-style-css' );
		wp_dequeue_style( 'woocommerce-blocks');
		wp_dequeue_style( 'woocommerce-general');
		wp_dequeue_style( 'woocommerce-layout' );
		wp_dequeue_style( 'woocommerce-smallscreen' );
		wp_dequeue_style( 'woocommerce_fancybox_styles' );
		wp_dequeue_style( 'woocommerce_chosen_styles' );
		wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
		wp_dequeue_script( 'selectWoo' );
		wp_deregister_script( 'selectWoo' );
		wp_dequeue_script( 'wc-add-payment-method' );
		wp_dequeue_script( 'wc-lost-password' );
		wp_dequeue_style ( 'wc-block-vendors-style-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-credit-card-form' );
		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( 'jquery-payment' );
		wp_dequeue_script( 'fancybox' );
		wp_dequeue_script( 'jqueryui' );
	}
}
add_action( 'wp_enqueue_scripts', 'grd_woocommerce_script_cleaner', 99 );

If you’re using Ezoic Leap, after implementing this change make sure to clear the cache inside the caching app and clear the cache in the Leap Optimization Settings tab as well (two caches to clear).


By Ezoic WordPres Dev Team

The Ezoic WordPress dev team is laser-focused on improving the WordPress experience for digital publishers. Multi-purpose themes combined with a multi-purpose CMS, like WordPress, have made the landscape overly complex. Our team is dedicated to making it better and easier.