How can I get the number of products in each category, for example, WooCommerce?

I am creating a new website and I really like Woocommerce. I just need a quick trick to get the number of products in each category. I already call a category for each product, but I can’t figure out how to get a product score from this category.

I have a list style for my products (really actions for an activity site). Check the image .

I just want to repeat the β€œaction” count next to the category. This is how I get my category:

echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); 

I tried to get an invoice using:

 $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); echo $numposts; 

But this repeats some strange number. I tried several variations of this query, calling for a product, etc.

[update]

This is what I was able to do:

 <li><?php $cat1 = $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); echo $cat1; /* $args = array( 'taxonomy' => 'product_cat' ); $terms = get_terms('product_cat', $args); echo count($terms); */ $args = array( 'post_type' => 'product', 'taxonomy' => $cat1[0] ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); echo count( $loop->post->ID ) endwhile; wp_reset_query(); // Remember to reset ?></li> 

But it actually counts all the products in all categories using increments of "1" .... So, instead of the echo reply "category: abc has a" 3 "product," it echoes "categories: abc has a" 1 1 1 1 1 1 1 "

I know that there is a simple filter that I can do here, I feel like I'm here.

+4
source share
2 answers

Okay, so thanks for putting me on the correct brasofilo track, I'm sure I have redundancy, but that gives me what I need.

 <?php $cat1 = $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '</span>' ); //get the category ID and permalink $term_list = wp_get_post_terms($post->ID,'product_cat',array('fields'=>'ids')); $cat_id = (int)$term_list[0]; $term = get_term( $cat_id, 'product_cat' ); //echo the category, product count, and link echo $cat1 . '<a href='. get_term_link ($cat_id, 'product_cat') .'>' . ' See all ' . $term- >count . ' Activities</a>'; ?> 
+1
source

Both get_term and get_terms will return objects that already contain the number of categories.

If you want to get all categories of WooCommerce products and print their message count:

 $terms = get_terms( 'product_cat' ); // DEBUG // var_dump( $terms ); foreach( $terms as $term ) { echo 'Product Category: ' . $term->name . ' - Count: ' . $term->count; } 

If you want to check only one category that you previously knew ID:

 $term = get_term( 16, 'product_cat' ); // <--- tested in my system with this ID echo 'Product Category: ' . $term->name . ' - Count: ' . $term->count; 
+8
source

Source: https://habr.com/ru/post/1490591/


All Articles