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( '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(); </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.