vendor/lnb/shopware6-lnb-cms-element/src/Subscriber/ReviewCountForListingSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Lnb\Shopware6\LnbCmsElement\Subscriber;
  4. use Lnb\Shopware6\LnbCmsElement\Service\ReviewCountHelper;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ReviewCountForListingSubscriber implements EventSubscriberInterface
  12. {
  13.     public const REVIEW_COUNT_EXTENSION_NAME 'review_count';
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  18.             ProductListingResultEvent::class => 'onProductListingResult',
  19.             ProductEvents::PRODUCT_SEARCH_CRITERIA => 'onProductSearchCriteria',
  20.             SearchPageLoadedEvent::class => 'onSearchPageLoaded',
  21.         ];
  22.     }
  23.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  24.     {
  25.         //add total review count
  26.         ReviewCountHelper::enrichCriteria($event->getCriteria());
  27.     }
  28.     public function onProductListingResult(ProductListingResultEvent $event): void
  29.     {
  30.         //map total review count to product
  31.         ReviewCountHelper::enrichSearchResult($event->getResult());
  32.     }
  33.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
  34.     {
  35.         //add total review count
  36.         ReviewCountHelper::enrichCriteria($event->getCriteria());
  37.     }
  38.     public function onSearchPageLoaded(SearchPageLoadedEvent $event): void
  39.     {
  40.         //add total review count
  41.         ReviewCountHelper::enrichSearchResult($event->getPage()->getListing());
  42.     }
  43. }