custom/plugins/SmsBecoTechnicTheme/src/Subscriber/ProductPageSubscriber.php line 25

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SmsBecoTechnicTheme\Subscriber;
  3. use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class ProductPageSubscriber implements EventSubscriberInterface
  7. {
  8.     private BlockCategoryService $blockCategoryService;
  9.     public function __construct(BlockCategoryService $blockCategoryService)
  10.     {
  11.         $this->blockCategoryService $blockCategoryService;
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             \Shopware\Storefront\Page\Product\ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  17.         ];
  18.     }
  19.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event) {
  20.         $product $event->getPage()->getProduct();
  21.         $categoryTree $product->getCategoryTree();
  22.         $customerId $event->getSalesChannelContext()->getCurrentCustomerGroup()->getId();
  23.         $blockedCategories $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($customerId$event->getContext());
  24.         foreach($categoryTree as $key => $category) {
  25.             if (in_array($category$blockedCategories)) {
  26.                 unset($categoryTree[$key]);
  27.             }
  28.         }
  29.         $product->setCategoryTree($categoryTree);
  30.     }
  31. }