How to get WooCommerce Product Categories and its children’s children?

enter image description here

I added a Checkbox field in the Product > Categories on my dashboard and I want to display the categories that is being checked but I can’t display the grand children of a parent category.

Here is my code:

function foobar_func( $atts ){
        $taxonomy     = 'product_cat';
        $orderby      = 'name';  
        $show_count   = 0;      // 1 for yes, 0 for no
        $pad_counts   = 0;      // 1 for yes, 0 for no
        $hierarchical = 1;      // 1 for yes, 0 for no  
        $title        = '';  
        $empty        = 0;
        $meta_key     = 'wh_meta_shop';

        $args = array(
               'taxonomy'     => $taxonomy,
               'orderby'      => $orderby,
               'show_count'   => $show_count,
               'pad_counts'   => $pad_counts,
               'hierarchical' => $hierarchical,
               'title_li'     => $title,
               'hide_empty'   => $empty,
               'meta_key'     => $meta_key
        );
        echo '<ul class="widget woocommerce widget_product_categories">';
        echo '<ul class="product-categories">';
         $all_categories = get_categories( $args );
         foreach ($all_categories as $cat) {
            if($cat->category_parent == 0) {
                $category_id = $cat->term_id;   
                echo '<li class="cat-parent"><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .' ('.$cat->count.')'.'</a>';

                $args2 = array(
                        'taxonomy'     => $taxonomy,
                        'child_of'     => 0,
                        'parent'       => $category_id,
                        'orderby'      => $orderby,
                        'show_count'   => $show_count,
                        'pad_counts'   => $pad_counts,
                        'hierarchical' => $hierarchical,
                        'title_li'     => $title,
                        'hide_empty'   => $empty
                );
                $sub_cats = get_categories( $args2 );
                if($sub_cats) {
                  echo '<ul class="children">';
                    foreach($sub_cats as $sub_category) {

                        echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .' ('.$sub_category->count.')'.'</a></li>';
                    }   
                  echo '</ul>';
                }
            }       
        }
        echo '</li></ul></ul>';

      }
      add_shortcode( 'foobar', 'foobar_func' );

I created a shortcode for this since I want to put it on my sidebar widget.

Edit: In this code I am successfully getting the category and the first level children only.

I’m unable to display test-category-inner, see photo below:

Read more here:: How to get WooCommerce Product Categories and its children’s children?

Leave a Reply

Your email address will not be published. Required fields are marked *