public

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

  • Download Gist
Woocommerce - show empty categories
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
// 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;
}
 
}

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

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.

// 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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.