WordPress.org

Support

Support » Plugins and Hacks » Show empty categories not working

Show empty categories not working

  • The show empty categories function doesn’t appear to be working since the latest update. The code in my functions file is:

    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;
    }

    This worked perfectly before. Anyone have any fixes for this?

    Thanks

    https://wordpress.org/plugins/woocommerce/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Your filter is being foiled by the woocommerce_product_subcategories() function. On line 1493 it says:

    if ( $category->parent != $parent_id || $category->count == 0 )
    					continue;

    So even though the empty category is being returned from the get_catgories() query, it is not being shown because the count is 0.

    Change that line to this:

    if ( $category->parent != $parent_id )
    					continue;

    and empty categories will show. Example: http://cl.ly/image/3r3T0c0G2Y2A

    I know it’s a core modification (frowned upon), but doesn’t look like there’s a way around it unless you rewrite that function.

    Added a pull request.

    Here’s a better description of the fix.

    Hi Daniel!

    Awesome, thanks for this…works perfectly! Has the wc-template-functions.php file been changed in the latest release? Causing the previous code to break?

    Thanks

    Yeah, it changed in 2.1.x. The 2.0.20 code was just checking $parent_id:

    $product_categories = get_categories( apply_filters( 'woocommerce_product_subcategories_args', $args ) );
    
    		$product_category_found = false;
    
    		if ( $product_categories ) {
    
    			foreach ( $product_categories as $category ) {
    
    				if ( $category->parent != $parent_id )
    					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
    				) );
    
    			}

    And the file used to be named woocommerce-template.php

    Right…ok. Thanks for your help, I’ll keep an eye on this for future releases!

    FamousCreative – Question: Why would you want to show empty categories? Clicking on the categories will go to an empty page: http://cl.ly/image/1j0A2u141U0W

    Is that intended?

    Yeah, it’s intentional. I’m hiding the ‘no products found’ message, and displaying different content, depending on the chosen category.

    It’s a bit of a hack to be honest, but the quickest solution I could think up, due to my client needing things to work this way. There’s probably a much better way to do this, but it works, and they’re happy, so I’m happy!

    I appreciate that under more normal usage, displaying empty categories probably isn’t actually necessary.

    Thanks, the core devs were asking this to figure out if my pull request was necessary for core.

    My solution for showing empty product categories was even more of a ugly hack, but maybe a helpful idea for somebody:

    – make a dummy product (not searchable) in the category
    – do a redirect permanent in your htaccess for the category’s url to another page. Maybe for the product itself a redirect would make sense, too.

    Only problem is, it throws you out of your shop path then.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Show empty categories not working’ is closed to new replies.