I want to remove Woocommerce scripts and styles from non woocommerce pages. I am trying code below but it's not working. I found the issue why it's not working, because woocommerce not setting priorty on add action. See woocommerce code here https://github.com/woocommerce/woocommerce/blob/44d129e45286b782fcce2ba67acf837a46e0c6c9/includes/class-wc-frontend-scripts.php#L43
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) );
FYI: I tried code below in my functions.php which didn't worked for me.
/**
* 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( '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_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', 999 );
Remove woocommerce scripts from no woocommerce pages
I found the solution, use the action below to remove WC scripts. I tested on Woocommerce Version 4.7.4 and it's working great.
add_action('template_redirect', 'conditionally_disable_wc_assets');
/**
* Remove WC stuff on non WC pages.
*/
function conditionally_disable_wc_assets()
{
if (IF_CONDTION_MET) {
// remove WC generator tag
remove_filter('get_the_generator_html', 'wc_generator_tag', 10, 2);
remove_filter('get_the_generator_xhtml', 'wc_generator_tag', 10, 2);
// unload WC scripts
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);
// remove "Show the gallery if JS is disabled"
remove_action('wp_head', 'wc_gallery_noscript');
}
}