custom/plugins/SmsBecoTechnicTheme/src/Subscriber/ListingSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace SmsBecoTechnicTheme\Subscriber;
  4. use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
  5. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. class ListingSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var BlockCategoryService
  15.      */
  16.     private $blockCategoryService;
  17.     public function __construct(BlockCategoryService $blockCategoryService)
  18.     {
  19.         $this->blockCategoryService $blockCategoryService;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             ProductListingCriteriaEvent::class => 'handleListingRequest',
  25.             ProductCrossSellingIdsCriteriaEvent::class => 'handleCrossSellingIdsCriteriaEvent'
  26.         ];
  27.     }
  28.     public function handleCrossSellingIdsCriteriaEvent(ProductCrossSellingIdsCriteriaEvent $event){
  29.         $event->getCriteria()->addAssociation('manufacturer');
  30.         $event->getCriteria()->addAssociation('manufacturer.media');
  31.     }
  32.     public function handleListingRequest(ProductListingCriteriaEvent $event): void
  33.     {
  34.         $event->getCriteria()->addAssociation('properties.group');
  35.         $event->getCriteria()->addAssociation('manufacturer');
  36.         $event->getCriteria()->addAssociation('manufacturer.media');
  37.         $customerGroup $event->getSalesChannelContext()->getCurrentCustomerGroup()->getId();
  38.         $blockedCategories $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($customerGroup$event->getContext());
  39.         if (count($blockedCategories) > && in_array($event->getSalesChannelContext()->getSalesChannel()->getNavigationCategoryId(), $blockedCategories)) {
  40.             $event->getCriteria()->addAssociation("categories")->addFilter(new NotFilter(NotFilter::CONNECTION_AND, [new EqualsAnyFilter('categories.id'$blockedCategories)]));
  41.         }
  42.     }
  43. }