How to Turn off / Disable Unneeded WooCommerce Gutenberg blocks

Spoiler alert – we are going to do that without plugins! 😁🎉

Turning off WooCommerce Gutenberg Blocks in Block Manager

I already talked about in here, actually you do not have to force disable Gutenberg blocks in your code. Anyway, before doing it, please consider turning off blocks in Block Manager which appeared in WordPress 5.2

WooCommerce blocks are also presented there:

Using Block Manager to turn off WooCommerce Gutenberg blocks
You can access Block Manager from the menu in the top right part of the screen.

Force Disable WooCommerce Gutenberg Blocks in the Code

At the moment of writing this tutorial WooCommerce do not have a filter hook to prevent some of the blocks from being registered, so I am going to show you how to do it with the help of allowed_block_types filter hook.

There is a whole tutorial about this filter hook on my website.

Anyway, it is not difficult for me to explain one more time how allowed_block_types works.

Example 1

The easy one, let’s suppose that’ve inserted this code into your (child) theme functions.php file:

add_filter( 'allowed_block_types', 'misha_allow_2_woo_blocks' );
 
function misha_allow_2_woo_blocks( $allowed_blocks ){
 
	$allowed_blocks = array(
		'woocommerce/handpicked-products',
		'woocommerce/product-on-sale',
	);
 
	return $allowed_blocks;
 
}

And the result:

Allow only 2 WooCommerce Gutenberg blocks with allowed_block_types filter hook

That’s too much, right? 😁

Example 2

As you probably understood from our previous example, when you’re using allowed_block_types, you have two options:

WP_Block_Type_Registry::get_instance()->get_all_registered() will help us with it. This is how:

add_filter( 'allowed_block_types', 'misha_allowed_blocks' );
 
function misha_allowed_blocks( $allowed_blocks ) {
 
	$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
 
 	// specify all the blocks you would like to disable here
	unset( $registered_blocks[ 'woocommerce/all-products' ] );
	unset( $registered_blocks[ 'woocommerce/all-reviews' ] );
	// etc ...
 
	// now $registered_blocks contains only blocks registered by plugins, but we need keys only
	$registered_blocks = array_keys( $registered_blocks );
 
	// merge the whitelist with plugins blocks
	$allowed_blocks = array_merge( 
		array(
			'core/image',
			'core/paragraph',
			'core/heading',
			'core/list',
		),
		$registered_blocks 
	);
 
	return $allowed_blocks;
 
}

Please let me explain that a little bit:

List of WooCommerce Gutenberg Block Names / Slugs

And here is the list of default Gutenberg block names/slugs.

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 11 yrs of experience.

Need some custom developer help? Get in touch