custom/plugins/AcrisCustomerGroupAvailableProductCS/src/Storefront/Subscriber/ProductSubscriber.php line 36

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\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. class ProductSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var DisplayAllowedProductService
  12.      */
  13.     private $displayProductService;
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $configService;
  18.     public function __construct(DisplayAllowedProductService $displayProductServiceSystemConfigService $configService)
  19.     {
  20.         $this->displayProductService $displayProductService;
  21.         $this->configService $configService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return[
  26.             ProductPageLoadedEvent::class => 'onProductLoaded'
  27.         ];
  28.     }
  29.     public function onProductLoaded(ProductPageLoadedEvent $event)
  30.     {
  31.         $productId $event->getPage()->getProduct()->getId();
  32.         if(empty($productId)) {
  33.             return;
  34.         }
  35.         if($this->configService->get('AcrisCustomerGroupAvailableProductCS.config.releaseProductsIfNoCustomerGroupAssigned'$event->getSalesChannelContext()->getSalesChannel()->getId()) === DisplayAllowedProductService::DEFAULT_PLUGIN_CONFIG_RELEASE_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED && $this->displayProductService->checkIfNoCustomerGroupsAssigned($productId$event->getSalesChannelContext()->getContext())) return;
  36.         $blockedProductIds $this->displayProductService->getDisplayProductIdsForCustomerGroupId($event->getSalesChannelContext()->getCurrentCustomerGroup()->getId(), $event->getContext());
  37.         if(!in_array($productId$blockedProductIds)) {
  38.             throw new NotFoundHttpException();
  39.         }
  40.     }
  41. }