Home » Php » php – Show All Empty Categories in Woocommerce Shop Page except Uncategorized

php – Show All Empty Categories in Woocommerce Shop Page except Uncategorized

Posted by: admin February 25, 2020 Leave a comment

Questions:

Woocommerce generally displays only the categories with products on the Shop page.
I would like to display all the categories(except Uncategorized) in the Shop Page even if it’s empty.

The below displays all the categories including Uncategorized. Is there a way to exclude Uncategorized from that?

add_filter( 'woocommerce_product_subcategories_hide_empty', 'show_empty_categories', 10, 1 );
function show_empty_categories ( $show_empty ) {
   $show_empty  =  true;   
    return $show_empty;
}
How to&Answers:

Change the args in the following way

https://github.com/woocommerce/woocommerce/blob/master/includes/wc-template-functions.php#L2479

function my_product_subcategories_arg( $args ) {
    $uncategorized = get_option( 'default_product_cat' );
    $args['exclude'] = $uncategorized;
    $args['hide_empty'] = 0;
    return $args;
}
add_filter( 'woocommerce_product_subcategories_args', 'my_product_subcategories_arg', 10, 1 );