'Remove sidebar from product categories which display type is subcategories in Woocommerce

I'd like to write a function that removes the sidebar from any product category page in woocommerce whose display type is subcategories.

Some kind of function that says if this category has display type subcategories then disappear the sidebar.

Any help is appreciated.



Solution 1:[1]

By default, woocommerce uses the default type (displays subcategories if they exist and products if there are no subcategories) To check the current value, use a conditional function:

woocommerce_products_will_display()

So you can remove the sidebar like this:

function remove_storefront_sidebar( $name ){
  if ( is_product_category() && !woocommerce_products_will_display() ){
    remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
  }
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');

Another option is to hide only if the display type is "subcategories":

function remove_storefront_sidebar( $name ){
  $display_type = woocommerce_get_loop_display_mode();
  if ( is_product_category() && 'subcategories' === $display_type ){
    remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
  }
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yaroslawww