<?php declare(strict_types=1);
namespace SmsBecoTechnicTheme\Subscriber;
use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageSubscriber implements EventSubscriberInterface
{
private BlockCategoryService $blockCategoryService;
public function __construct(BlockCategoryService $blockCategoryService)
{
$this->blockCategoryService = $blockCategoryService;
}
public static function getSubscribedEvents(): array
{
return [
\Shopware\Storefront\Page\Product\ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
];
}
public function onProductPageLoadedEvent(ProductPageLoadedEvent $event) {
$product = $event->getPage()->getProduct();
$categoryTree = $product->getCategoryTree();
$customerId = $event->getSalesChannelContext()->getCurrentCustomerGroup()->getId();
$blockedCategories = $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($customerId, $event->getContext());
foreach($categoryTree as $key => $category) {
if (in_array($category, $blockedCategories)) {
unset($categoryTree[$key]);
}
}
$product->setCategoryTree($categoryTree);
}
}