'product’,
'posts_per_page’ => -1,
'fields’ => 'ids’, // Only retrieve product IDs to improve performance
);
// Check if on a product category page and get the category
if ( is_product_category() ) {
$category = get_queried_object();
if ($category && !empty($category->term_id)) {
// If category object is valid and has an ID, modify the query to filter by this category
$args[’tax_query’] = array(
array(
'taxonomy’ => 'product_cat’,
'field’ => 'term_id’,
'terms’ => $category->term_id,
),
);
} else {
// If no valid category, return (can also handle error or do nothing)
return; // Stop execution to prevent further errors
}
}
// Run the query
$query = new WP_Query($args);
// Output the product count
if ( is_product_category() && isset($category) ) {
echo $query->found_posts; // Number of products in this category
} elseif ( is_shop() ) {
echo $query->found_posts; // Total number of products in the shop
}
}
?>
pozycji