custom/plugins/AcrisCustomerGroupAvailableProductCS/src/Storefront/Subscriber/ProductListingFeaturesSubscriber.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CustomerGroupAvailableProduct\Storefront\Subscriber;
  3. use Acris\CustomerGroupAvailableProduct\Components\DisplayAllowedProductService;
  4. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductCrossSellingStreamCriteriaEvent;
  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\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductListingFeaturesSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var DisplayAllowedProductService
  21.      */
  22.     private $displayProductService;
  23.     /**
  24.      * @var SystemConfigService
  25.      */
  26.     private $configService;
  27.     public function __construct(DisplayAllowedProductService $displayProductServiceSystemConfigService $configService)
  28.     {
  29.         $this->displayProductService $displayProductService;
  30.         $this->configService $configService;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             ProductListingCriteriaEvent::class => [
  36.                 ['handleListingRequest'200]
  37.             ],
  38.             ProductSuggestCriteriaEvent::class => [
  39.                 ['handleSearchRequest'200]
  40.             ],
  41.             ProductSearchCriteriaEvent::class => [
  42.                 ['handleSearchRequest'200]
  43.             ],
  44.             ProductCrossSellingIdsCriteriaEvent::class => [
  45.                 ['handleCrossSellingProductListingRequest'200]
  46.             ],
  47.             ProductCrossSellingStreamCriteriaEvent::class => [
  48.                 ['handleCrossSellingProductStreamRequest'200]
  49.             ]
  50.         ];
  51.     }
  52.     public function handleCrossSellingProductStreamRequest(ProductCrossSellingStreamCriteriaEvent $event): void
  53.     {
  54.         $this->addDisplayProductIdsForCriteria($event->getCriteria(), $event->getSalesChannelContext());
  55.     }
  56.     public function handleCrossSellingProductListingRequest(ProductCrossSellingIdsCriteriaEvent $event): void
  57.     {
  58.         $this->addDisplayProductIdsForCriteria($event->getCriteria(), $event->getSalesChannelContext());
  59.     }
  60.     public function handleListingRequest(ProductListingCriteriaEvent $event): void
  61.     {
  62.         $this->addDisplayProductIdsForCriteria($event->getCriteria(), $event->getSalesChannelContext());
  63.     }
  64.     public function handleSearchRequest(ProductListingCriteriaEvent $event): void
  65.     {
  66.         $this->addDisplayProductIdsForCriteria($event->getCriteria(), $event->getSalesChannelContext());
  67.     }
  68.     private function addDisplayProductIdsForCriteria(Criteria $criteriaSalesChannelContext $salesChannelContext)
  69.     {
  70.         $displayProductIds $this->displayProductService->getDisplayProductIdsForCustomerGroupId($salesChannelContext->getCurrentCustomerGroup()->getId(), $salesChannelContext->getContext());
  71.         if(empty($displayProductIds) === true) {
  72.             if($this->configService->get('AcrisCustomerGroupAvailableProductCS.config.releaseProductsIfNoCustomerGroupAssigned'$salesChannelContext->getSalesChannel()->getId()) === DisplayAllowedProductService::DEFAULT_PLUGIN_CONFIG_RELEASE_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED) {
  73.                 $criteria->addAssociation('product.customerGroup');
  74.                 $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  75.                     new EqualsFilter('id'Uuid::randomHex()),
  76.                     new EqualsFilter('product.customerGroup.id'null),
  77.                 ]));
  78.                 return;
  79.             }
  80.             $criteria->addFilter(new EqualsFilter('id'Uuid::randomHex()));
  81.         } else {
  82.             $criteria->addFilter(new EqualsAnyFilter('id'$displayProductIds));
  83.         }
  84.     }
  85. }