Create a gist now

Instantly share code, notes, and snippets.

Show empty categories within Woocommerce. Paste this in to your theme's functions file. It's just a copy and paste of a function within woocommerce-template.php and changing value of hide_empty to 0
// Paste this in to your theme's functions file
// Redefine sub category display to output empty categories
function woocommerce_product_subcategories( $args = array() ) {
global $woocommerce, $wp_query, $_chosen_attributes;
$defaults = array(
'before' => '',
'after' => '',
'force_display' => false
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
// Main query only
if ( ! is_main_query() && ! $force_display ) return;
// Don't show when filtering
if ( sizeof( $_chosen_attributes ) > 0 || ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) ) return;
// Don't show when searching or when on page > 1 and ensure we're on a product archive
if ( is_search() || is_paged() || ( ! is_product_category() && ! is_shop() ) ) return;
// Check cateogries are enabled
if ( is_product_category() && get_option( 'woocommerce_show_subcategories' ) == 'no' ) return;
if ( is_shop() && get_option( 'woocommerce_shop_show_subcategories' ) == 'no' ) return;
// Find the category + category parent, if applicable
if ( $product_cat_slug = get_query_var( 'product_cat' ) ) {
$product_cat = get_term_by( 'slug', $product_cat_slug, 'product_cat' );
$product_category_parent = $product_cat->term_id;
} else {
$product_category_parent = 0;
}
// NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( http://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work
$args = array(
'child_of' => $product_category_parent,
'menu_order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'taxonomy' => 'product_cat',
'pad_counts' => 1
);
$product_categories = get_categories( $args );
$product_category_found = false;
if ( $product_categories ) {
foreach ( $product_categories as $category ) {
if ( $category->parent != $product_category_parent )
continue;
if ( ! $product_category_found ) {
// We found a category
$product_category_found = true;
echo $before;
}
woocommerce_get_template( 'content-product_cat.php', array(
'category' => $category
) );
}
}
// If we are hiding products disable the loop and pagination
if ( $product_category_found == true && get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) {
$wp_query->post_count = 0;
$wp_query->max_num_pages = 0;
}
if ( $product_category_found ) {
echo $after;
return true;
}
}
@ctpweb
ctpweb commented Dec 13, 2012

Great, just what I needed! Do you also have a solution for the product categories widget to show empty categories? Cheers!

@scrumbleyj

Wow, thank you so much! I notice that this addition makes products show up on the shop page, even though my settings are correct to make them not show up. I have tried editing this addition, but I can't get the products to go away. Would it be possible for you to help me edit this to prevent products from appearing on the shop page? I would really appreciate it! The shop is http://www.pacsglobal.com/shop which only has 2 categories and 1 item until I can get this issue handled.

@dalemoore

// WooCommerce Code
add_filter('woocommerce_product_subcategories_args', 'woocommerce_show_empty_categories');
function woocommerce_show_empty_categories($cat_args){
$cat_args['hide_empty']=0;
return $cat_args;
}

In your functions.php is all you need... not all of this.

@juslintek

All of that big chunk of function works for me, and filter doesn't... :-( Guess I'll stay with large chunk.

@eklingen88

Better yet, since version 2.2.6 released on 08/10/2014:

add_filter('woocommerce_product_subcategories_hide_empty', 'woocommerce_show_empty_categories', 10, 1);

function woocommerce_show_empty_categories($show_empty){
    return true;
}

@dalemoore's solution would have worked on old versions of WooCommerce, but at some point they switched over to this new woocommerce_product_subcategories_hide_empty filter, which occurs after the categories are retrieved with the arguments provided by the woocommerce_product_subcategories_args filter.

@StingerDevLab

You can actually use predefined core function __return_true(), so the whole thing transforms into one line of code in your functions.php:

add_filter( 'woocommerce_product_subcategories_hide_empty', '__return_true');
@mekatrig
mekatrig commented Sep 6, 2016

StingerDevlab.
Yup that's the fix, nice & simple
Still works as of this post with WP 4.6 & WooCommerce 2.6.4
I just wish they would put a 'show empty categories' button, enough people are calling for it to warrant it's inclusion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment