I am using the following code:
/* Woocommerce - Add Product Count View in Each Category */
add_action( 'woocommerce_before_shop_loop', 'add_product_count_view', 20);
function add_product_count_view() {
$terms = get_the_terms( $post->ID, 'product_cat');
foreach( $terms as $term ) {
if(is_tax('product_cat', $term->name)) {
echo '<span class="count-view">'.$term->count
.__( ' items')
.'</span>';
}
}
}
This code shows the total items in the store of the categories.
If a category has 20 items, 15 active and 5 inactive, it shows that there are 20 items in that category.
What I need is that it only shows the number of active articles in that category.
How can I fix it?
2
Answers
You can get the count of products published on the product category page visited via the
WC_Product_Query
class (using thewc_get_products
function):The code has been tested and works. Add it to your active theme’s functions.php.
Easy solution is
wc_get_loop_prop( 'total' )
, which is returns total products of any WooCommerce archive pages.