S
S
SergZay2015-11-18 11:50:45
WordPress
SergZay, 2015-11-18 11:50:45

How to show number of products near woocommerce category?

I want to display the woocommerce category menu, and display the number of products next to it.

<?php
  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 1;
  $pad_counts   = 1; 
  $hierarchical = 1; 
  $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
);
?>
<?php $all_categories = get_categories( $args );
// print_r($all_categories);
foreach ($all_categories as $cat) {
    // print_r($cat);
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;

?>      
<?php echo $show_count; ?>
<?php  echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; ?>


        <?php
        $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) {
        	$sub_category_id = $sub_category->term_id;
                echo '<br /><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';

// 
        $args3 = array(
          'taxonomy'     => $taxonomy,
          'child_of'     => 0,
          'parent'       => $sub_category_id,
          'orderby'      => $orderby,
          'show_count'   => $show_count,
          'pad_counts'   => $pad_counts,
          'hierarchical' => $hierarchical,
          'title_li'     => $title,
          'hide_empty'   => $empty
        );
        $sub_two_cats = get_categories( $args3 );
        if($sub_two_cats) {
            foreach($sub_two_cats as $sub_two_category) {
            	
                echo '<br /><a href="'. get_term_link($sub_two_category->slug, 'product_cat') .'">'. $sub_two_category->name .'</a>';
                }
            }
// 


            }

        } ?>

    <?php }     
}
?>

Link

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question