4

I'm working on a custom WooCommerce theme, and ideally would like to use the stable Gutenberg blocks shipped with WooCommerce. Obviously, instead of over-riding all of their styles with my own to match the design, I would rather remove them to improve page speed. Seems like it should be a simple process, but no result so far. Using this:

function mytheme_woocommerce_remove_block_styles() {
  wp_dequeue_style( 'wc-block-style-css' );
}
add_action( 'wp_enqueue_scripts', 'mytheme_woocommerce_remove_block_styles', 100 );

Do I need to hook it at a higher priority? I would think 100 would be sufficient. Any assistance is appreciated.

6
  • By the way, where did you add this code? maybe you added in inside a class? Commented Jun 29, 2021 at 6:55
  • Not in a class. It’s in a dedicated woocommerce file that’s required in functions.php.
    – DavidBrown
    Commented Jun 29, 2021 at 13:53
  • Try moving the code directly to functions.php, if it works than you are probably not requiring the file Commented Jun 29, 2021 at 14:27
  • Everything else in the file is firing, so it’s being required correctly. But tried it in functions.php to confirm…still nothing. Something has to be over-riding it…
    – DavidBrown
    Commented Jun 30, 2021 at 4:32
  • Download query monitor, this plugin will allow you to see what styles/scripts are being loaded and where they are loaded from. Maybe that way you could find the right action Commented Jun 30, 2021 at 4:59

6 Answers 6

11

I found out a working solution with Woocommerce 8.0.1 in 2023:

function disable_wp_blocks() {
    $wstyles = array(
        'wp-block-library',
        'wc-blocks-style',
        'wc-blocks-style-active-filters',
        'wc-blocks-style-add-to-cart-form',
        'wc-blocks-packages-style',
        'wc-blocks-style-all-products',
        'wc-blocks-style-all-reviews',
        'wc-blocks-style-attribute-filter',
        'wc-blocks-style-breadcrumbs',
        'wc-blocks-style-catalog-sorting',
        'wc-blocks-style-customer-account',
        'wc-blocks-style-featured-category',
        'wc-blocks-style-featured-product',
        'wc-blocks-style-mini-cart',
        'wc-blocks-style-price-filter',
        'wc-blocks-style-product-add-to-cart',
        'wc-blocks-style-product-button',
        'wc-blocks-style-product-categories',
        'wc-blocks-style-product-image',
        'wc-blocks-style-product-image-gallery',
        'wc-blocks-style-product-query',
        'wc-blocks-style-product-results-count',
        'wc-blocks-style-product-reviews',
        'wc-blocks-style-product-sale-badge',
        'wc-blocks-style-product-search',
        'wc-blocks-style-product-sku',
        'wc-blocks-style-product-stock-indicator',
        'wc-blocks-style-product-summary',
        'wc-blocks-style-product-title',
        'wc-blocks-style-rating-filter',
        'wc-blocks-style-reviews-by-category',
        'wc-blocks-style-reviews-by-product',
        'wc-blocks-style-product-details',
        'wc-blocks-style-single-product',
        'wc-blocks-style-stock-filter',
        'wc-blocks-style-cart',
        'wc-blocks-style-checkout',
        'wc-blocks-style-mini-cart-contents',
        'classic-theme-styles-inline'
    );

    foreach ( $wstyles as $wstyle ) {
        wp_deregister_style( $wstyle );
    }

    $wscripts = array(
        'wc-blocks-middleware',
        'wc-blocks-data-store'
    );

    foreach ( $wscripts as $wscript ) {
        wp_deregister_script( $wscript );  
    }
}

add_action( 'init', 'disable_wp_blocks', 100 );
5
  • Thank you! The entire internet is out of date on this one... damn WooCommerce for this difficulty! Commented Aug 19, 2023 at 16:20
  • Brilliant. Couldn't figure out why the usual method was not working in a new WC installation (v8+). This did the trick perfectly.
    – omega33
    Commented Aug 22, 2023 at 7:23
  • This is completely ridiculous that we have to do this. Thank you! Commented Sep 6, 2023 at 13:43
  • 1
    Thank you for providing this answer. I am getting fed up every time we run a Google PageSpeed check that our site plummets from 90/100 to 50/100 because WordPress and Woocommerce keep adding bloated trash that we don't need.
    – MrCarrot
    Commented Sep 7, 2023 at 15:36
  • Another thank you from me. Woo is absolutely killing itself for me. It's like the folks at Automattic haven't noticed how important page speed is to the web over the last...twenty years?
    – Nathan
    Commented Jan 18 at 22:51
3

after trying all kinds of versions of the dequeue code, this mix is what worked for me:

function ca_deregister_woocommerce_block_styles() {
    wp_deregister_style( 'wc-blocks-style' );
    wp_dequeue_style( 'wc-blocks-style' );
}
add_action( 'enqueue_block_assets', 'ca_deregister_woocommerce_block_styles' );
1

i used this and worked for me Tested 100% ! Enjoy add that with the aid of Code Snippets plugin or manually by adding to functions.php

if(!function_exists('kazhol_remove_block_styles_woo')){
function kazhol_remove_block_styles_woo() {
    wp_deregister_style( 'wc-blocks-style' );
    wp_dequeue_style( 'wc-blocks-style' );
}
}add_action( 'enqueue_block_assets', 'kazhol_remove_block_styles_woo' );
0

The dequeue handle is incorrect

it should be wp_dequeue_style('wc-block-style'); and not wp_dequeue_style('wc-block-style-css');

A bit confusing at first i agree, every js and css that was enqued via wp_enqueue_style or wp_enqueue_script gets a suffix of css/js

Example

wp_enqueue_style('swiper', 'https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.min.css')

This will create a <link> with an id attribute of swiper-css, im guessing this is why you thought that in order to dequeue the style you need to do this fundtion wp_dequeue_style('swiper-css'). But it should be wp_dequeue_style('swiper')

9
  • There's nothing about this on the official docs, and the examples do not do this, are you sure this is a general thing and not specific to a framework or blocks?
    – Tom J Nowell
    Commented Jun 28, 2021 at 13:16
  • @TomJNowell If you view page source/consle you can see that the id attribute of styles and scripts always ends in -js or -css. If you name your css handle my-style, in the front it would be my-style-css. This is easy to replicate. I know about this because I had the same problem when I started using wordpress =]. Again, this only happens if you use wp_enqueue_style or wp_enqueue_script Commented Jun 28, 2021 at 13:18
  • that's not the handle though, that's just added to the HTML tags ID to prevent clashes if a script and a style have the same handle, query monitor can be used to inspect the actual handles.
    – Tom J Nowell
    Commented Jun 28, 2021 at 13:30
  • @TomJNowell Thats what im saying. OP tried to remove wc-block-style-css but that handle does not exists. Because wordpress added -css to the end. I'm sure im not explaining myself correctly XD Commented Jun 28, 2021 at 13:31
  • can you edit your question to make this clearer? Right now your second and 3rd paragraphs contradict eachother by saying the opposite thing
    – Tom J Nowell
    Commented Jun 28, 2021 at 13:32
0

I just came across this question, and the handle for the block-style is actually wc-blocks-style

From the HTML Head

<link rel="stylesheet" id="wc-blocks-style-css" href="https://c0.wp.com/p/woocommerce/5.6.0/packages/woocommerce-blocks/build/wc-blocks-style.css" type="text/css" media="all">

So to dequeue the script, you need to use this.

function mytheme_woocommerce_remove_block_styles() {
    wp_dequeue_style( 'wc-blocks-style' ); // WooCommerce Blocks
}
add_action( 'wp_enqueue_scripts', 'mytheme_woocommerce_remove_block_styles', 100 );
0

A simplified version of Dariusz's answer to remove WC block style assets for WooCommerce 8+ with future proofing for new blocks would be:

add_action(
    'wp_enqueue_scripts',
    function() {
        global $wp_styles;
        foreach ( $wp_styles->queue as $handle ) {
            if ( str_starts_with( $handle, 'wc-blocks' ) ) {
                wp_deregister_style( $handle );
                wp_dequeue_style( $handle );
            }
        }
    },
    999
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.