src/Core/Subscriber/ProductVisibilitySubscriber.php line 102

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\Core\Subscriber;
  4. use Bodymed\Webshop\Constants;
  5. use Bodymed\Webshop\Core\Content\CustomerProductFeature\CustomerProductFeatureEntity;
  6. use Bodymed\Webshop\Core\Content\Product\SalesChannel\Product\CustomerProductVisibilityFilter;
  7. use Shopware\Core\Checkout\Customer\CustomerEntity;
  8. use Shopware\Core\Content\Product\Events\ProductCrossSellingCriteriaEvent;
  9. use Shopware\Core\Content\Product\Events\ProductCrossSellingStreamCriteriaEvent;
  10. use Shopware\Core\Content\Product\Events\ProductGatewayCriteriaEvent;
  11. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  12. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  13. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  14. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  15. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  16. use Shopware\Core\Content\Product\Events\ProductSuggestResultEvent;
  17. use Shopware\Core\Content\Product\ProductEntity;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  20. use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageCriteriaEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. /**
  23.  * @see https://madcode.atlassian.net/browse/BODYMED-297
  24.  */
  25. class ProductVisibilitySubscriber implements EventSubscriberInterface
  26. {
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             /*ProductListingResultEvent::class => 'onProductListingResult',
  31.             ProductSearchResultEvent::class => 'onProductSearchResult',
  32.             ProductSuggestResultEvent::class => 'onProductSuggestResult',*/
  33.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  34.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteria',
  35.             ProductSuggestCriteriaEvent::class => 'onProductSuggestCriteria',
  36.             ProductCrossSellingCriteriaEvent::class => 'onProductCrossSellingCriteria',
  37.             ProductCrossSellingStreamCriteriaEvent::class => 'onProductCrossSellingStreamCriteria',
  38.             ProductGatewayCriteriaEvent::class => 'onProductGatewayCriteria',
  39.             ProductPageCriteriaEvent::class => 'onProductPageCriteria',
  40.             MinimalQuickViewPageCriteriaEvent::class => 'onMinimalQuickViewPageCriteria',
  41.         ];
  42.     }
  43.     public function onProductListingResult(ProductListingResultEvent $event): void
  44.     {
  45.         #$this->hideInvisibleProducts($event);
  46.     }
  47.     public function onProductSearchResult(ProductSearchResultEvent $event): void
  48.     {
  49.         #$this->hideInvisibleProducts($event);
  50.     }
  51.     public function onProductSuggestResult(ProductSuggestResultEvent $event): void
  52.     {
  53.         #$this->hideInvisibleProducts($event);
  54.     }
  55.     private function hideInvisibleProducts(ProductListingResultEvent $event): void
  56.     {
  57.         /* @var $product ProductEntity */
  58.         foreach ($event->getResult() as $product) {
  59.             if ($product->hasExtension(Constants::CUSTOMER_PRODUCT_FEATURE_EXTENSION_KEY)) {
  60.                 /* @var $customerProductFeature CustomerProductFeatureEntity */
  61.                 $customerProductFeature $product->getExtension(Constants::CUSTOMER_PRODUCT_FEATURE_EXTENSION_KEY);
  62.                 if ($customerProductFeature instanceof CustomerProductFeatureEntity
  63.                     && $customerProductFeature->getVisible() === false
  64.                 ) {
  65.                     $event->getResult()->remove($customerProductFeature->getProductId());
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     public function onMinimalQuickViewPageCriteria(MinimalQuickViewPageCriteriaEvent $event): void
  71.     {
  72.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  73.             $this->addCustomerProductFeatureCriteria(
  74.                 $event->getCriteria(),
  75.                 $event->getSalesChannelContext()->getCustomer()
  76.             );
  77.         }
  78.     }
  79.     public function onProductCrossSellingCriteria(ProductCrossSellingCriteriaEvent $event): void
  80.     {
  81.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  82.             $this->addCustomerProductFeatureCriteria(
  83.                 $event->getCriteria(),
  84.                 $event->getSalesChannelContext()->getCustomer()
  85.             );
  86.         }
  87.     }
  88.     public function onProductCrossSellingStreamCriteria(ProductCrossSellingStreamCriteriaEvent $event): void
  89.     {
  90.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  91.             $this->addCustomerProductFeatureCriteria(
  92.                 $event->getCriteria(),
  93.                 $event->getSalesChannelContext()->getCustomer()
  94.             );
  95.         }
  96.     }
  97.     public function onProductGatewayCriteria(ProductGatewayCriteriaEvent $event): void
  98.     {
  99.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  100.             $this->addCustomerProductFeatureCriteria(
  101.                 $event->getCriteria(),
  102.                 $event->getSalesChannelContext()->getCustomer()
  103.             );
  104.         }
  105.     }
  106.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  107.     {
  108.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  109.             $this->addCustomerProductFeatureCriteria(
  110.                 $event->getCriteria(),
  111.                 $event->getSalesChannelContext()->getCustomer()
  112.             );
  113.         }
  114.     }
  115.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
  116.     {
  117.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  118.             $this->addCustomerProductFeatureCriteria(
  119.                 $event->getCriteria(),
  120.                 $event->getSalesChannelContext()->getCustomer()
  121.             );
  122.         }
  123.     }
  124.     public function onProductSuggestCriteria(ProductSuggestCriteriaEvent $event): void
  125.     {
  126.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  127.             $this->addCustomerProductFeatureCriteria(
  128.                 $event->getCriteria(),
  129.                 $event->getSalesChannelContext()->getCustomer()
  130.             );
  131.         }
  132.     }
  133.     public function onProductPageCriteria(ProductPageCriteriaEvent $event): void
  134.     {
  135.         if ($event->getSalesChannelContext()->getCustomer() !== null) {
  136.             $this->addCustomerProductFeatureCriteria(
  137.                 $event->getCriteria(),
  138.                 $event->getSalesChannelContext()->getCustomer()
  139.             );
  140.         }
  141.     }
  142.     private function addCustomerProductFeatureCriteria(Criteria $criteriaCustomerEntity $customer): void
  143.     {
  144.         $customerId $customer->getId();
  145.         $criteria->addAssociation(Constants::CUSTOMER_PRODUCT_FEATURE_EXTENSION_KEY);
  146.         $criteria->addFilter(new CustomerProductVisibilityFilter($customerId));
  147.     }
  148. }