custom/plugins/AcrisCategoryCustomerGroupCS/src/Subscriber/ProductListingFeaturesSubscriber.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CategoryCustomerGroup\Subscriber;
  3. use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
  4. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ProductListingFeaturesSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var BlockCategoryService
  17.      */
  18.     private $blockCategoryService;
  19.     /**
  20.      * @var SystemConfigService
  21.      */
  22.     private $configService;
  23.     public function __construct(BlockCategoryService $blockCategoryServiceSystemConfigService $configService)
  24.     {
  25.         $this->blockCategoryService $blockCategoryService;
  26.         $this->configService $configService;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             ProductListingCriteriaEvent::class => [
  32.                 ['handleListingRequest'200]
  33.             ],
  34.             ProductSuggestCriteriaEvent::class => [
  35.                 ['handleSearchRequest'200]
  36.             ],
  37.             ProductSearchCriteriaEvent::class => [
  38.                 ['handleSearchRequest'200]
  39.             ]
  40.         ];
  41.     }
  42.     public function handleListingRequest(ProductListingCriteriaEvent $event): void
  43.     {
  44.         if(!$this->configService->get('AcrisCategoryCustomerGroupCS.config.hideAssignedProductsForOtherCategories'$event->getSalesChannelContext()->getSalesChannel()->getId())) {
  45.             return;
  46.         }
  47.         $this->addBlockedCategoryIdsForCriteria($event->getCriteria(), $event->getSalesChannelContext());
  48.     }
  49.     public function handleSearchRequest(ProductListingCriteriaEvent $event): void
  50.     {
  51.         if(!$this->configService->get('AcrisCategoryCustomerGroupCS.config.hideAssignedProductsForSearch'$event->getSalesChannelContext()->getSalesChannel()->getId())) {
  52.             return;
  53.         }
  54.         $this->addBlockedCategoryIdsForCriteria($event->getCriteria(), $event->getSalesChannelContext());
  55.     }
  56.     private function addBlockedCategoryIdsForCriteria(Criteria $criteriaSalesChannelContext $salesChannelContext)
  57.     {
  58.         $blockedCategoryIds $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($salesChannelContext->getCurrentCustomerGroup()->getId(), $salesChannelContext->getContext());
  59.         if(empty($blockedCategoryIds) === true) {
  60.             return;
  61.         }
  62.         $criteria->addFilter(new NotFilter(NotFilter::CONNECTION_AND, [new EqualsAnyFilter('product.categories.id'$blockedCategoryIds)]));
  63.     }
  64. }