Created
October 21, 2012 06:33
-
-
Save simonlk/3926139 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} |
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');
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
Works for me better and more simple:
Code for WooCommerce 3.x+
function hide_empty_categories ( $hide_empty ) {
$hide_empty = FALSE;
// You can add other logic here too
return $hide_empty;
}
add_filter( 'woocommerce_product_subcategories_hide_empty', 'hide_empty_categories', 10, 1 );
Pre 3.x Code
function show_empty_categories ( $show_empty ) {
$show_empty = TRUE;
// You can add other logic here too
return $show_empty;
}
add_filter( 'woocommerce_product_subcategories_hide_empty', 'show_empty_categories', 10, 1 );
aliomaster, thanks a lot!
almazka987 Thanks. That worked like a charm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better yet, since version 2.2.6 released on 08/10/2014:
@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 thewoocommerce_product_subcategories_args
filter.