WordPress.org

Forums

WooCommerce - excelling eCommerce
Show empty categories not working (12 posts)

  1. FamousCreative
    Member
    Posted 1 year ago #

    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/

  2. Daniel Espinoza
    Member
    Posted 1 year ago #

    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

  3. Daniel Espinoza
    Member
    Posted 1 year ago #

    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.

  4. Daniel Espinoza
    Member
    Posted 1 year ago #

    Added a pull request.

    Here's a better description of the fix.

  5. FamousCreative
    Member
    Posted 1 year ago #

    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

  6. Daniel Espinoza
    Member
    Posted 1 year ago #

    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
    				) );
    
    			}
  7. Daniel Espinoza
    Member
    Posted 1 year ago #

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

  8. FamousCreative
    Member
    Posted 1 year ago #

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

  9. Daniel Espinoza
    Member
    Posted 1 year ago #

    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?

  10. FamousCreative
    Member
    Posted 1 year ago #

    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.

  11. Daniel Espinoza
    Member
    Posted 1 year ago #

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

  12. rollbrettfahrer77
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.