phpwordpresswoocommerce

get woocommerce categories with subcategory


I want to get all woocommerce category's in front-end with subcategory like this result:

<ul>
    <li><a href="">Link</a>
        <ul>
            <li><a href="">Submenu link</a></li>
        </ul>
    </li>
</ul>

Here is what i have (But it's Not what i want) :

<?php

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

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

        $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) {
                foreach($sub_cats as $sub_category) {
                    echo  '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
                }
            }
        }       
}
?>  

this code show's category and subcategory but the subcategory is not where should be, the subcategory is like separate link like this:

<ul>
    <li><a href="">link</a></li>
    <li><a href="">Submenu link</a></li>
</ul>

Solution

  • You could try this code:

      $args = array(
              'taxonomy' => 'product_cat',
              'hide_empty' => false,
              'parent'   => 0
          );
      $product_cat = get_terms( $args );
    
      foreach ($product_cat as $parent_product_cat)
      {
    
      echo '
          <ul>
            <li><a href="'.get_term_link($parent_product_cat->term_id).'">'.$parent_product_cat->name.'</a>
            <ul>
              ';
      $child_args = array(
                  'taxonomy' => 'product_cat',
                  'hide_empty' => false,
                  'parent'   => $parent_product_cat->term_id
              );
      $child_product_cats = get_terms( $child_args );
      foreach ($child_product_cats as $child_product_cat)
      {
        echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
      }
    
      echo '</ul>
          </li>
        </ul>';
      }
    

    This will Print in your WooCommerce, Wordpress based site.