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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductCustomerGroup\Storefront\Subscriber;
  3. use Acris\ProductCustomerGroup\Components\BlockProductService;
  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 BlockProductService
  12.      */
  13.     private $blockProductService;
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $configService;
  18.     public function __construct(BlockProductService $blockProductServiceSystemConfigService $configService)
  19.     {
  20.         $this->blockProductService $blockProductService;
  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('AcrisProductCustomerGroupCS.config.blockProductsIfNoCustomerGroupAssigned'$event->getSalesChannelContext()->getSalesChannel()->getId()) === BlockProductService::DEFAULT_PLUGIN_CONFIG_BLOCK_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED && $this->blockProductService->checkIfNoCustomerGroupsAssigned($productId$event->getSalesChannelContext()->getContext())) {
  36.             throw new NotFoundHttpException();
  37.         }
  38.         $blockedProductIds $this->blockProductService->getBlockedProductIdsForCustomerGroupId($event->getSalesChannelContext()->getCurrentCustomerGroup()->getId(), $event->getContext());
  39.         if(in_array($productId$blockedProductIds)) {
  40.             throw new NotFoundHttpException();
  41.         }
  42.     }
  43. }