Instantly share code, notes, and snippets.

Embed
What would you like to do?
Only load WooCommerce scripts on shop pages and checkout + cart
/**
* Optimize WooCommerce Scripts
* Remove WooCommerce Generator tag, styles, and scripts from non WooCommerce pages.
*/
add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );
function child_manage_woocommerce_styles() {
//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' );
}
}
}
@DevinWalker

This comment has been minimized.

Owner

DevinWalker commented Nov 19, 2014

I've removed the opening PHP tag so people won't be confused about that.

@tomherold

This comment has been minimized.

tomherold commented Feb 3, 2015

After adding this code I still see 3 CSS scripts loaded...

@tingsao

This comment has been minimized.

tingsao commented Feb 18, 2015

@DevinWalker Thank you. Do you add this to functions.php?

@tingsao

This comment has been minimized.

tingsao commented Feb 18, 2015

@AF4Q

This comment has been minimized.

AF4Q commented Apr 5, 2015

I guess, this code needs some updating as it doesnt remove everything with the latest version of WooCommerce.

@APlusDesign

This comment has been minimized.

APlusDesign commented May 21, 2015

IF you are using latest version of Woo, you need to add these three lines to remove ALL styles

wp_dequeue_style( 'woocommerce-general' );
            wp_dequeue_style( 'woocommerce-layout' );
            wp_dequeue_style( 'woocommerce-smallscreen' );
@cnasikas

This comment has been minimized.

cnasikas commented Oct 3, 2015

Or you can remove all styles at once

add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
@foamymedia

This comment has been minimized.

foamymedia commented Feb 4, 2016

this doesnt seem to work for site working on - https://officialicloudunlock.co.uk ? any ideas?

@landsman

This comment has been minimized.

landsman commented Aug 1, 2016

Thanks @cnasikas, CSS are good to remove :)

I try remove only few JS rows but not works.

@jaswrks

This comment has been minimized.

jaswrks commented Jan 25, 2017

Here's what worked for me. Removes all styles & scripts.

if (!is_woocommerce() && !is_cart() && !is_checkout()) {
    remove_action('wp_enqueue_scripts', [WC_Frontend_Scripts::class, 'load_scripts']);
    remove_action('wp_print_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
    remove_action('wp_print_footer_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
}
@alenabdula

This comment has been minimized.

alenabdula commented Feb 23, 2017

Following worked for me to dequeue both styles and scripts. While example above by @jaswsinc didn't.

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

function 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' );
        }
    }
}
@Basilou38

This comment has been minimized.

Basilou38 commented Jan 18, 2018

Hi @DevinWalker

I apply your code, thanks for sharing, indeed, woocommerce is faster!

does your code remove the ajax function from the "add to cart" button ? Because I use a plugin (woo popup) to create an animation when we add a product to the cart, this plugin uses ajax (otherwise it does not work), my problem is that if I use your code, my plugin
" woo popup "does not work well, there is a conflict.

Do you have a solution to let ajax activate ?

@Basilou38

This comment has been minimized.

Basilou38 commented Feb 21, 2018

up

@blondiegeek

This comment has been minimized.

blondiegeek commented Feb 27, 2018

Good info - any pointers on how to modify the above code so that the woocommerce geolocation functionality doesn't get called for a given page? We need this on our site's store pages, but it's extremely slow and so we'd of course like to avoid calling it on all of the non-store pages.

For clarity, I'm talking about the "geolocate with page caching support", that results in URLs with parameters like ?v=11aedd0e4327 appended.

Thanks in advance for your help!

@RiFi2k

This comment has been minimized.

RiFi2k commented May 16, 2018

Big props to @jaswrks, you saved me from rooting through api documentation. All scripts 100% gone, 3 lines of code.

remove_action('wp_enqueue_scripts', [WC_Frontend_Scripts::class, 'load_scripts']);
remove_action('wp_print_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
remove_action('wp_print_footer_scripts', [WC_Frontend_Scripts::class, 'localize_printed_scripts'], 5);
@gtamborero

This comment has been minimized.

gtamborero commented Sep 3, 2018

Oh god! that was dificult! Thanks @alenabdula . Your code inside functions.php works perfectly ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment