custom/static-plugins/MadProductVisibility/src/Subscriber/ProductSubscriber.php line 98

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadProductVisibility\Subscriber;
  4. use MadProductVisibility\Struct\NotEqualsAnyFilter;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  9. use Shopware\Core\Content\Product\ProductEntity;
  10. use Shopware\Core\Content\Product\ProductEvents;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NandFilter;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NorFilter;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\SingleFieldFilter;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\XOrFilter;
  22. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class ProductSubscriber implements EventSubscriberInterface
  25. {
  26.     private EntityRepositoryInterface $productRepository;
  27.     private EntityRepositoryInterface $tagRepository;
  28.     public function __construct(
  29.         EntityRepositoryInterface $productRepository,
  30.         EntityRepositoryInterface $tagRepository
  31.     ) {
  32.         $this->productRepository $productRepository;
  33.         $this->tagRepository $tagRepository;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  38.         return [
  39.             #ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded',
  40.             ProductPageCriteriaEvent::class => 'onProductPageCriteriaEvent',
  41.             ProductListingCriteriaEvent::class => 'onProductListingCriteriaEvent',
  42.             ProductSuggestCriteriaEvent::class => 'onProductSuggestCriteriaEvent',
  43.             ProductEvents::PRODUCT_SEARCH_CRITERIA => 'onProductSearchCriteriaEvent',
  44.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteriaEvent',
  45.         ];
  46.     }
  47.     public function onProductListingCriteriaEvent(ProductListingCriteriaEvent $event): void
  48.     {
  49.         $criteria $event->getCriteria();
  50.         $criteria->addAssociation('madProductRoleBlacklist');
  51.         $customer $event->getSalesChannelContext()->getCustomer();
  52.         if ($customer === null || $customer->getTagIds() === null) {
  53.             return;
  54.         }
  55.         $this->addBlacklistedTagsFilter($customer$criteria);
  56.     }
  57.     public function onProductPageCriteriaEvent(ProductPageCriteriaEvent $event): void
  58.     {
  59.         $criteria $event->getCriteria();
  60.         $criteria->addAssociation('madProductRoleBlacklist');
  61.         $customer $event->getSalesChannelContext()->getCustomer();
  62.         if ($customer === null || $customer->getTagIds() === null) {
  63.             return;
  64.         }
  65.         $this->addBlacklistedTagsFilter($customer$criteria);
  66.     }
  67.     public function onProductSearchCriteriaEvent(ProductSearchCriteriaEvent $event): void
  68.     {
  69.         $criteria $event->getCriteria();
  70.         $criteria->addAssociation('madProductRoleBlacklist');
  71.         $customer $event->getSalesChannelContext()->getCustomer();
  72.         if ($customer === null || $customer->getTagIds() === null) {
  73.             return;
  74.         }
  75.         $this->addBlacklistedTagsFilter($customer$criteria);
  76.     }
  77.     public function onProductSuggestCriteriaEvent(ProductListingCriteriaEvent $event): void
  78.     {
  79.         $criteria $event->getCriteria();
  80.         $criteria->addAssociation('madProductRoleBlacklist');
  81.         $customer $event->getSalesChannelContext()->getCustomer();
  82.         if ($customer === null || $customer->getTagIds() === null) {
  83.             return;
  84.         }
  85.         $this->addBlacklistedTagsFilter($customer$criteria);
  86.     }
  87.     private function addBlacklistedTagsFilter(CustomerEntity $customerCriteria $criteria): void
  88.     {
  89.         $customerTags $customer->getTagIds();
  90.         $blacklistedTags = [];
  91.         foreach ($customerTags as $customerTagId) {
  92.             $blacklistedTags[] = new EqualsFilter('madProductRoleBlacklist.id'$customerTagId);
  93.         }
  94.         $criteria->addFilter(
  95.             new NorFilter(
  96.                 $blacklistedTags
  97.             )
  98.         );
  99.     }
  100.     public function onProductsLoaded(EntityLoadedEvent $event): void
  101.     {
  102.         return;
  103.     }
  104. }