<?php
declare(strict_types=1);
namespace FourtwosixThemeCustomization\Subscriber;
use FourtwosixThemeCustomization\FourtwosixThemeCustomization as ThemeCustomization;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent as ListingEvent;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService as ConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
use Shopware\Core\Content\Product\SalesChannel\Filter\Filter;
class ListingCriteriaSubscriber implements EventSubscriberInterface
{
/** @var ConfigService $configService */
private ConfigService $configService;
/** @var EntityRepository $categoryRepository */
private EntityRepository $categoryRepository;
public function __construct(
ConfigService $configService,
EntityRepository $categoryRepository
) {
$this->configService = $configService;
$this->categoryRepository = $categoryRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductListingCriteriaEvent::class => 'onListingCriteria',
];
}
public function onListingCriteria(ListingEvent $event): void
{
$navigationId = $event->getRequest()->get('navigationId');
// $isDebug = $event->getRequest()->get('fourtwosixDump');
$hiddenCategoryId = $this->configService->get(ThemeCustomization::HIDDEN_CATEGORY_ID);
$affectedCategoryId = $this->configService->get(ThemeCustomization::AFFECTED_CATEGORY_ID);
if (
empty($hiddenCategoryId) ||
$navigationId !== $affectedCategoryId
) {
return;
}
$criteria = $event->getCriteria();
$criteria->addAssociation('categoriesRo');
$criteria->addFilter(new NotFilter(
NotFilter::CONNECTION_OR,
[new EqualsAnyFilter('product.categoriesRo.id', [$hiddenCategoryId])]
));
// $filters = [new ContainsFilter('categoryIds', $hiddenCategoryId),];
// if ($this->configService->get(ThemeCustomization::HIDE_SUB_CATEGORIES)) {
// $context = $event->getContext();
// $subCategories = $this->getSubCategories($context, $hiddenCategoryId);
// foreach ($subCategories as $subCategory) {
// $filters[] = new ContainsFilter('categoryIds', $subCategory['id']);
// }
// }
// $event->getCriteria()->addFilter(new NotFilter(
// NotFilter::CONNECTION_OR,
// $filters
// ));
// if($isDebug) {
// }
}
private function getSubCategories(Context &$context, string &$categoryId): array
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('parentId', $categoryId));
return $this->categoryRepository->searchIds($criteria, $context)->getData();
}
}