Unload WooCommerce assets on non-shop pages
- WooCommerce
WooCommerce loads its stylesheets and JavaScript on every page of your site — blog posts, landing pages, about pages — even when there are no products or cart functionality present. This adds 100-200 KB of unused assets that slow down every non-shop page.
<?php
/**
* Unload WooCommerce Assets on Non-Shop Pages
*
* WooCommerce loads its CSS and JS on every page of your site, even pages
* that have nothing to do with the shop. This adds ~100-200 KB of unused
* assets to your blog posts, landing pages, and other non-commerce content.
* This snippet dequeues WooCommerce assets on pages that don't need them.
*
* Usage: Add to functions.php. Adjust conditions if you display products
* on custom pages (e.g. with shortcodes or page builders).
*
* @category woocommerce
* @language php
*
* @author Adame Dahmani https://adame.io
* @support Support my family project: https://adame.io/fundraiser
*/
add_action( 'wp_enqueue_scripts', function () {
if ( is_admin() ) {
return;
}
if (
is_woocommerce() ||
is_cart() ||
is_checkout() ||
is_account_page() ||
is_product() ||
is_product_category() ||
is_product_tag()
) {
return;
}
// Dequeue styles.
wp_dequeue_style( 'woocommerce-general' );
wp_dequeue_style( 'woocommerce-layout' );
wp_dequeue_style( 'woocommerce-smallscreen' );
wp_dequeue_style( 'wc-blocks-style' );
// Dequeue scripts.
wp_dequeue_script( 'wc-cart-fragments' );
wp_dequeue_script( 'woocommerce' );
wp_dequeue_script( 'wc-add-to-cart' );
}, 99 );The snippet checks if the current page is WooCommerce-related (shop, product, cart, checkout, account) and only loads assets there. If you display products on custom pages using shortcodes or page builder widgets, add those conditions to the guard clause.