<?php
declare(strict_types=1);
namespace SmsBecoTechnicTheme\Subscriber;
use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class ListingSubscriber implements EventSubscriberInterface
{
/**
* @var BlockCategoryService
*/
private $blockCategoryService;
public function __construct(BlockCategoryService $blockCategoryService)
{
$this->blockCategoryService = $blockCategoryService;
}
public static function getSubscribedEvents()
{
return [
ProductListingCriteriaEvent::class => 'handleListingRequest',
ProductCrossSellingIdsCriteriaEvent::class => 'handleCrossSellingIdsCriteriaEvent'
];
}
public function handleCrossSellingIdsCriteriaEvent(ProductCrossSellingIdsCriteriaEvent $event){
$event->getCriteria()->addAssociation('manufacturer');
$event->getCriteria()->addAssociation('manufacturer.media');
}
public function handleListingRequest(ProductListingCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('properties.group');
$event->getCriteria()->addAssociation('manufacturer');
$event->getCriteria()->addAssociation('manufacturer.media');
$customerGroup = $event->getSalesChannelContext()->getCurrentCustomerGroup()->getId();
$blockedCategories = $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($customerGroup, $event->getContext());
if (count($blockedCategories) > 0 && in_array($event->getSalesChannelContext()->getSalesChannel()->getNavigationCategoryId(), $blockedCategories)) {
$event->getCriteria()->addAssociation("categories")->addFilter(new NotFilter(NotFilter::CONNECTION_AND, [new EqualsAnyFilter('categories.id', $blockedCategories)]));
}
}
}