PHP Developer

Monday, 14 December 2015

Get product category list in woocommerce Wordpress

Get product category list in woocommerce :

<?php
echo "<div class='product_cat_sidebar'>";
echo "<h4>Wholesale Categories</h4>";
$taxonomy     = 'product_cat';
  $orderby      = 'ID';
  $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,
         'order'      => "asc",
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
$all_categories = get_categories( $args );
echo "<ul class='sidebar_cat'>";
foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;
        if(isset($_GET["product_cat"]) && $cat->slug == $_GET["product_cat"]){
            echo '<li class="active_category"><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
        } else {
            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  $sub_category->name ;
            } 
        }
    }
}
echo "</ul>";
echo "</div>";

?>

No comments:

Post a Comment