Skip to content

Instantly share code, notes, and snippets.

@DevinWalker
Last active January 8, 2024 13:24
Show Gist options
  • Save DevinWalker/7621777 to your computer and use it in GitHub Desktop.
Save DevinWalker/7621777 to your computer and use it in GitHub Desktop.
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' );
}
}
}
@petkovtsvetelin
Copy link

It's not working anymore. Any tips?

@CatinhoCR
Copy link

CatinhoCR commented Nov 25, 2020

@DevinWalker @jaswrks @Basilou38
Im wondering how y'all manage to replace all those built in WC scripts (and any plugins) with custom code tho?
Dev-Time wise seems a bit harsh.. The cart updating, the order generation from checkout, all the store notices, messages and most importantly form fields -> form submission seem to be heavily depend on those scripts.. Just wondering, any tips here r highly appreciated if there's some nice workflow or "shortcuts"

P.S: Still relevant today.
You could also do (functions.php)

add_filter('woocommerce_enqueue_styles', '__return_empty_array');

for CSS in 1 line pretty much.. it's in the Theming Docs.

Side Note:
This still seems to work for me with all latest versions of WP & WC...
Don't forget to add it inside a function, and then add_action to invoke it...

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' );
function gist_rm_wc_scripts() {
   if (function_exists('is_woocommerce') {
     # wp_dequeue above here
   }
}
add_action('wp_enqueue_scripts', 'gist_rm_wc_scripts');

@theshane9
Copy link

Hey guys, I have disable WooCommerce styles and scripts sitewide as I need good performance and it was killing my site performance. I have an issue now where the login form and other account forms have no styling. How can I target the account pages of members so that it displays the styling only for any user account related pages?

@srikat
Copy link

srikat commented Mar 7, 2022

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