Display product categories (WooCommerce) in a structured way (Parent / Child)

Hello,

I am currently working on a WooCommcere store.

I am creating a sidebar that will contain a structured overview of all categories and their subcategories.

My construct consists of a Nested Looper: provider for the parent category -> provider for subcategories.

The looper provider for the parent categories is already working. Only the parent catgeories (parent =>0) are displayed.

However, the query for the subcategories does not quite work. Although the looper provider is in the consumer of the parent categories, the subcategories of the currently opened category are always shown. Thus, all subcategories are repeated.

How can I customize the looper to show the matching subcategories of the corresponding parent elements?

Thank you!

Hello @mrdplusd,

Thanks for writing to us.

It might not be possible with the Looper, but it would require a custom Looper provider. Please have a look at this doc to learn more about how to create a custom looper.

In case you have an idea about the coding please have look at these threads for reference.

Alternatively, you can use the widget called Product category List, you need to use the element called the Widget area then you need to go to the widgets and add the Product category list widget in the selected widget area.
Test-Page-Cornerstone-(93)

Test-Page-Cornerstone-(95)
Widgets---Themeco---WordPress-(1)

Hope it helps
Thanks

Thank Prakash, I have already worked my way through here.

I use the following query to output the parent category:

 add_filter('cs_looper_custom_parentcat', function($result, $args) {
    	
    	$cat_args=get_categories(
    	array(
    	'taxonomy'=> 'product_cat',
    	'hide_empty'=> 1,
    	'parent' => 0,
    	'orderby' => 'name',
        'order' => 'ASC'
    ));
    	
    return $cat_args;
    }, 10, 2);

And the following query to output the subcategories.

add_filter('cs_looper_custom_subcat', function($result, $args) {
    
    $cat_args = get_categories(array(
        'taxonomy'=> 'product_cat',
        'hide_empty'=> 1,
        'parent'=> get_queried_object_id(),
        'orderby' => 'name',
        'order' => 'ASC'
    ));

    return $cat_args;
}, 10, 2);

Independently of each other, both queries work smoothly.

But how do I configure the looper construct to output the subcategories of each parent category?

Hello Prakash,

now a few days have passed. Can you help me further? I’m afraid I can’t get any further without your help.

Thank you very much!

Hello @mrdplusd,

The given custom Looper Provider code can only be used on an archive page to display the subcategories of the current parent category archive page. You can not use the code to display product categories for your sidebar. Use the Product Category widgets instead.

Best Regards.

Thanks for your answer, but unfortunately this does not help me. I do not want to use a widget. I cannot style these as flexibly as I would like.

Searching for this topic in your forum or Facebook group, it is clear that many users are looking for such a solution.

I used to be thrilled with your support. That gave me security. Nowadays, I just feel like I’m on my own. You guys just don’t make an effort anymore.

If it wasn’t for the strong pro community on Facebook helping each other out, many of your users would be lost. Do you guys ever read it? If so, you’ll see that many users are about to switch to other themes for future projects.

I get it, support costs time and money. That’s why I was willing and had tried your One subscription. But even there the quality of support was not better than here in the forum.

I received equally short and terse answers that ultimately got me nowhere. The marketing messages around One don’t match at all with what the user gets in the end.

So please don’t recommend me now, as you do by default, to subscribe to One.

Hello @mrdplusd,

What you have in mind goes beyond the limits of the Loopers that have been suggested in the previous responses. The provided code will either display the Parent Categories only or the subcategories when you are viewing a parent category archive page. You cannot combine or nest those two Looper Provider Custom to achieve what you have in mind. It will not work.

What you can do is update the cs_looper_custom_subcat code into this:

add_filter('cs_looper_custom_subcat', function($result, $args){
    $parent_cat = cs_dynamic_content($args['parent_cat']);
    
    $cat_args = get_categories(
    array( 
      'parent' => $parent_cat,
      'taxonomy' => 'product_cat',
      'hide_empty' => 0,
    )
    );

    $subcats = $cat_args;
    return $subcats;
    }, 10, 2);

And then in your Looper Provider Custom Params, you use this:

The output would be something like:

Be advised that custom coding is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may need to hire 3rd party developer to sort things out.

Thank you for your understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.