I want the product review's total count on the product listing page like the product detail page how can I get that count on the listing page?
This isn't quite so trivial. You'll need to write a plugin to achieve this.
In your plugin you'll need to create a subscriber and listen to events related to product search and listing. You then need to alter both the search criteria by adding an aggregation, as well as the search result where you set aggregated values to the listed product objects.
class CustomListingEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductListingCriteriaEvent::class => [
['handleListingCriteria', -101],
],
ProductSearchCriteriaEvent::class => [
['handleListingCriteria', -101],
],
ProductListingResultEvent::class => [
['handleListingResult', 0],
],
ProductSearchResultEvent::class => [
['handleListingResult', 0],
],
];
}
public function handleListingCriteria(ProductListingCriteriaEvent $event): void
{
$criteria = $event->getCriteria();
$criteria->addAggregation(
new TermsAggregation('review_count', 'product.id', null, null, new CountAggregation('review_count', 'product.productReviews.id'))
);
}
public function handleListingResult(ProductListingResultEvent $event): void
{
/** @var TermsResult $aggregation */
foreach ($event->getResult()->getAggregations() as $aggregation) {
if ($aggregation->getName() === 'review_count') {
foreach ($aggregation->getBuckets() as $bucket) {
/** @var SalesChannelProductEntity $product */
$product = $event->getResult()->getEntities()->get($bucket->getKey());
if (!$product) {
continue;
}
// Ideally you should implement you own `Struct` extension
$text = new TextStruct();
$text->setContent((string) $bucket->getResult()->getCount());
$product->addExtension('review_count', $text);
}
}
}
}
}
For this example it will count all reviews associated to a product, not just those that are active. You might want to too look at the documentation on how to filter aggregations.
Afterwards in the template for the product boxes, you should be able to output the count of reviews per product:
{{ product.extensions.review_count.content }}