<?php
declare(strict_types=1);
namespace Lnb\Shopware6\LnbCmsElement\Subscriber;
use Lnb\Shopware6\LnbCmsElement\Service\ReviewCountHelper;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ReviewCountForListingSubscriber implements EventSubscriberInterface
{
public const REVIEW_COUNT_EXTENSION_NAME = 'review_count';
public static function getSubscribedEvents(): array
{
return [
ProductListingCriteriaEvent::class => 'onProductListingCriteria',
ProductListingResultEvent::class => 'onProductListingResult',
ProductEvents::PRODUCT_SEARCH_CRITERIA => 'onProductSearchCriteria',
SearchPageLoadedEvent::class => 'onSearchPageLoaded',
];
}
public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
{
//add total review count
ReviewCountHelper::enrichCriteria($event->getCriteria());
}
public function onProductListingResult(ProductListingResultEvent $event): void
{
//map total review count to product
ReviewCountHelper::enrichSearchResult($event->getResult());
}
public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
{
//add total review count
ReviewCountHelper::enrichCriteria($event->getCriteria());
}
public function onSearchPageLoaded(SearchPageLoadedEvent $event): void
{
//add total review count
ReviewCountHelper::enrichSearchResult($event->getPage()->getListing());
}
}