WooCommerce - Display nested list of all subcategories on archive-product.php

admin2025-06-06  10

When a user clicks on a product category from the WooCommerce shop page, instead of the default, I need it to look like the below design, the second picture marks what I mean:

So basically, I need to iterate through each subcategory, display it, check it it has subcategories, and, if it does, display them. I'm currently able to get the sub-subcategory level using the answer from this post and hooking it to "woocommerce_after_subcategory":

I just need help checking to see if those subcategories have a subcategory and then displaying them.

Please let me know if I need to further explain something! Thanks!

When a user clicks on a product category from the WooCommerce shop page, instead of the default, I need it to look like the below design, the second picture marks what I mean:

So basically, I need to iterate through each subcategory, display it, check it it has subcategories, and, if it does, display them. I'm currently able to get the sub-subcategory level using the answer from this post and hooking it to "woocommerce_after_subcategory":

https://wordpress.stackexchange/a/101273

I just need help checking to see if those subcategories have a subcategory and then displaying them.

Please let me know if I need to further explain something! Thanks!

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jan 26, 2016 at 19:16 asdfasdf 211 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I solved this by modifying the function linked to in the question to make it recursive. The comments explain my changes:

function woocommerce_subcats_from_parentcat_by_ID( $parent_cat_ID ) {
$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => $parent_cat_ID,
   'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);

if ( $subcats ) { // added if statement to check if there are subcategories
  echo '<ul class="product-category-list">';
    foreach ($subcats as $sc) {
      $link = get_term_link( $sc->slug, $sc->taxonomy );
        echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
        woocommerce_subcats_from_parentcat_by_ID( $sc->term_id ); // function calls itself
    }
  echo '</ul>';
} else {
  return; //return if no subcategories
    }} // last bracket kept being pushed out of code block if I used a line break

I'm sure there is a more efficient way to do this instead of having the function call itself every time but this works.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749198144a317187.html

最新回复(0)