Disabling CSS styles of WooCommerce blocks

Disabling CSS styles of WooCommerce blocks

WooCommerce is one of the most popular eCommerce plugins for WordPress. By default, it provides various useful blocks for the Block editor, including styles for these blocks.

Styles are loaded in a front-end and a back-end of your site. However, there might be situations when you need to disable these WooCommerce block styles.

While working on the Matthew theme, which is mainly based on WooCommerce and the Block editor, we’ve been experimenting with disabling CSS styles of WooCommerce blocks.

Our goal was to completely change a design of WooCommerce blocks because we wanted to make sure that these styles match the theme design.

Reasons to disable the styles

Of course, we can override default styles of WooCommerce blocks in the theme stylesheet but there are two main things that stop us to do it:

  • The theme will load styles that are not actually needed.
  • There will be a lot of CSS overrides which causes more work and makes more mess.

So, instead of overriding WooCommerce block styles, we’ve decided to completely remove them from the theme. Below, you can find a solution on how disable these styles in your own WordPress site.

Disable WooCommerce front-end styles

WooCommerce loads block styles when you or your users visit the site. So, you do not need to worry about styling WooCommerce blocks because it is already done for you.

If you do not need these styles on your WordPress site then you can disable it using this code snippet:

PHP
/**
 * Disable WooCommerce block styles (front-end).
 */
function themesharbor_disable_woocommerce_block_styles() {
  wp_dequeue_style( 'wc-blocks-style' );
}
add_action( 'wp_enqueue_scripts', 'themesharbor_disable_woocommerce_block_styles' );

Disable WooCommerce back-end styles

WooCommerce loads block styles in a back-end of your site as well. So, you can use these blocks in Gutenberg (Block editor).

The only difference is that it loads two style files instead of one: block editor styles and block styles.

It can be disabled using this code snippet:

PHP
/**
 * Disable WooCommerce block styles (back-end).
 */
function themesharbor_disable_woocommerce_block_editor_styles() {
  wp_deregister_style( 'wc-block-editor' );
  wp_deregister_style( 'wc-blocks-style' );
}
add_action( 'enqueue_block_assets', 'themesharbor_disable_woocommerce_block_editor_styles', 1, 1 );

Before adding these code snippets to your site, make sure you’ve created your own styles for WooCommerce blocks.

Otherwise, these blocks will be unstyled.

Add code snippets

To add code snippets in WordPress, use the Code Snippets plugin. This plugin allows to add custom code snippets to a WordPress website without modifying the parent files of the theme.

Alternatively, you can use the functions.php file of your child theme. I’ve mentioned a child theme because it’s highly not recommended to modify the parent files of your theme.

Be very careful when adding a custom code snippet to the functions.php file because any error or typo made in this file can break your site.

If you don’t have a coding experience then I would recommend to use the plugin mentioned above.


14 responses

  1. Ray Avatar
    Ray

    Hi,
    Can you tell me where I need to add those code, please.
    Thank you
    Bests regards,
    Ray.

    1. Taras Dashkevych Avatar

      Hi Ray,
      I’ve updated the post to provide additional information on how to add these code snippets to your site.
      Kind regards,
      Taras

  2. Cyrille Avatar
    Cyrille

    hello,
    thank’s for the tricks. I’m a beginner : where shall I write this code (in wich file) ?

    1. Taras Dashkevych Avatar

      Hello Cyrille,
      I’ve just update a post with more information on how to add these code snippets to your site.
      Kind regards,
      Taras

  3. japo_little_girl Avatar
    japo_little_girl

    Thank you so much!
    I’ve spent the whole day trying to figure out how to do this!
    It worked perfectly! <3

  4. Anton Nb Avatar
    Anton Nb

    Hello taras

    Does this functions also eliminate render blocking issue on pagespeed insight? I have lots of woocomerce css blocking

    1. Taras Dashkevych Avatar

      Hello Anton,

      These functions are designed only for disabling styles used for default WooCommerce blocks. So, it might eliminate some of CSS blocking issues which are caused by default block styles (if any) but it will not solve all the WooCommerce CSS blocking issues on your site.

      Kind regards,
      Taras

  5. Renardi Avatar
    Renardi

    Hi,
    This is no longer working in WooCommerce 5.5.1.

    So we can’t remove it. This seems like a dumb decision by WooCommerce team to enforce their CSS.

    But I found the solution for WooCommerce 5.5+

    Instead of wp_dequeue_style(), you use wp_deregister_style().

    1. Taras Dashkevych Avatar

      Hello Renardi,
      Thank you for providing a solution on how to resolve the problem.

      Hmm… I’ve just tried a solution using wp_dequeue_style() and it works fine on WordPress 5.8 and WooCommerce 5.7. However, I’ll keep your comment as an alternative solution for others who also cannot remove WooCommerce block styles using a solution provided in the article.

      Kind regards,
      Taras

  6. Pras Avatar
    Pras

    Hi there, thanks for this snippet. However, with the recent Woocommerce (5.7.1), the hook name is using plural ‘s‘.

    wp_dequeue_style( 'wc-blocks-style' );

    instead of

    wp_dequeue_style( 'wc-block-style' );

    Cheers

    1. Taras Dashkevych Avatar

      Hello Pras,

      Thank you very much for noticing it. The Block editor, and its related plugins, are changing rapidly, but I am glad that you’ve let us know about the change! I’ve just updated the article using a proper ID of WooCommerce block styles.

      Warm regards,
      Taras

  7. Ben Avatar
    Ben

    Works Amazing!! 10/10

    Smart Codes! 🙂

  8. nam3z Avatar
    nam3z

    It looks like WordPress 6.3 + WooCommerce 8.0.1 have some other IDs or something, because there are a lot of css files in from path
    “/wp-content/plugins/woocommerce/packages/woocommerce-blocks/build/*.css”

  9. Gilles Dumas Avatar
    Gilles Dumas

    I use this code :

    /**
    * Remove from the queue of CSS files to be inserted, those declared by WC for blocks (Gutenberg).
    * There are about 37 of them.
    *
    * @author Gilles Dumas
    * @since 20230904
    * @return void
    */
    function lc_disable_wc_blocks_scripts() {
    global $wp_styles;
    foreach ( $wp_styles->queue as $v) {
    if ( strpos( $v, ‘wc-blocks’ ) !== false ) {
    wp_dequeue_style( $v );
    }
    }
    }
    add_action( ‘wp_enqueue_scripts’, ‘lc_disable_wc_blocks_scripts’, 99 );

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.