custom/plugins/AcrisCategoryCustomerGroupCS/src/Storefront/Subscriber/ProductSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CategoryCustomerGroup\Storefront\Subscriber;
  3. use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
  4. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  6. use Shopware\Core\Framework\Struct\ArrayEntity;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. class ProductSubscriber implements EventSubscriberInterface
  13. {
  14.     public const HIDE_SEARCH_AND_SUGGEST 'acrisCategoryCustomerGroupSearchAndSuggest';
  15.     /**
  16.      * @var BlockCategoryService
  17.      */
  18.     private $blockCategoryService;
  19.     /**
  20.      * @var SystemConfigService
  21.      */
  22.     private $configService;
  23.     public function __construct(BlockCategoryService $blockCategoryServiceSystemConfigService $configService)
  24.     {
  25.         $this->blockCategoryService $blockCategoryService;
  26.         $this->configService $configService;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return[
  31.             ProductPageLoadedEvent::class => 'onProductLoaded',
  32.             ProductPageCriteriaEvent::class => 'onProductLoaderCriteria',
  33.             ProductSuggestCriteriaEvent::class => 'onProductSuggestCriteria',
  34.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteria'
  35.         ];
  36.     }
  37.     public function onProductLoaderCriteria(ProductPageCriteriaEvent $event): void
  38.     {
  39.         $event->getCriteria()->addAssociation('categories');
  40.     }
  41.     public function onProductSuggestCriteria(ProductSuggestCriteriaEvent $event): void
  42.     {
  43.         $event->getSalesChannelContext()->addExtension(self::HIDE_SEARCH_AND_SUGGEST, new ArrayEntity([]));
  44.     }
  45.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
  46.     {
  47.         $event->getSalesChannelContext()->addExtension(self::HIDE_SEARCH_AND_SUGGEST, new ArrayEntity([]));
  48.     }
  49.     public function onProductLoaded(ProductPageLoadedEvent $event): void
  50.     {
  51.         if(!$this->configService->get('AcrisCategoryCustomerGroupCS.config.hideAssignedProductsForOtherCategories'$event->getSalesChannelContext()->getSalesChannel()->getId())) {
  52.             return;
  53.         }
  54.         $product $event->getPage()->getProduct();
  55.         if(empty($product)) {
  56.             return;
  57.         }
  58.         $blockedCategoryIds $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($event->getSalesChannelContext()->getCurrentCustomerGroup()->getId(), $event->getContext());
  59.         if (!empty($product->getCategories()) && $product->getCategories()->count() > 0) {
  60.             foreach ($product->getCategories()->getElements() as $key => $category) {
  61.                 if(in_array($key$blockedCategoryIds)) {
  62.                     throw new NotFoundHttpException();
  63.                 }
  64.                 if($this->configService->get('AcrisCategoryCustomerGroupCS.config.blockCategoriesIfNoCustomerGroupAssigned'$event->getSalesChannelContext()->getSalesChannel()->getId()) === BlockCategoryService::DEFAULT_PLUGIN_CONFIG_BLOCK_CATEGORY_IF_NO_CUSTOMER_GROUPS_ASSIGNED && $this->blockCategoryService->checkIfNoCustomerGroupsAssigned($category->getId(), $event->getSalesChannelContext()->getContext())) throw new NotFoundHttpException();
  65.             }
  66.         }
  67.     }
  68. }