Halloween Sale 15% off Ends in See the plans
PHP

Load WooCommerce Assets Only on Shop Pages

Stops WooCommerce stylesheets and scripts from loading on pages that are not part of the store, such as blog posts and the about page.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'wp_enqueue_scripts', function () {
    if ( ! function_exists( 'is_woocommerce' ) ) {
        return;
    }

    if ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) {
        return;
    }

    foreach ( array( 'woocommerce-general', 'woocommerce-layout', 'woocommerce-smallscreen' ) as $handle ) {
        wp_dequeue_style( $handle );
    }

    foreach ( array( 'wc-cart-fragments', 'woocommerce', 'wc-add-to-cart' ) as $handle ) {
        wp_dequeue_script( $handle );
    }
}, 99 );

Notes

Do not use this if non-shop pages show products, a mini cart or add-to-cart buttons.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.