custom/plugins/FourtwosixThemeCustomization/src/Subscriber/ListingCriteriaSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace FourtwosixThemeCustomization\Subscriber;
  4. use FourtwosixThemeCustomization\FourtwosixThemeCustomization as ThemeCustomization;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent as ListingEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService as ConfigService;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
  17. use Shopware\Core\Content\Product\SalesChannel\Filter\Filter;
  18. class ListingCriteriaSubscriber implements EventSubscriberInterface
  19. {
  20.     /** @var ConfigService $configService */
  21.     private ConfigService $configService;
  22.     /** @var EntityRepository $categoryRepository */
  23.     private EntityRepository $categoryRepository;
  24.     public function __construct(
  25.         ConfigService $configService,
  26.         EntityRepository $categoryRepository
  27.     ) {
  28.         $this->configService $configService;
  29.         $this->categoryRepository $categoryRepository;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ProductListingCriteriaEvent::class => 'onListingCriteria',
  35.         ];
  36.     }
  37.     public function onListingCriteria(ListingEvent $event): void
  38.     {
  39.         $navigationId $event->getRequest()->get('navigationId');
  40.         // $isDebug = $event->getRequest()->get('fourtwosixDump');
  41.         $hiddenCategoryId $this->configService->get(ThemeCustomization::HIDDEN_CATEGORY_ID);
  42.         $affectedCategoryId $this->configService->get(ThemeCustomization::AFFECTED_CATEGORY_ID);
  43.         if (
  44.             empty($hiddenCategoryId) ||
  45.             $navigationId !== $affectedCategoryId
  46.         ) {
  47.             return;
  48.         }
  49.         $criteria $event->getCriteria();
  50.         $criteria->addAssociation('categoriesRo');
  51.         $criteria->addFilter(new NotFilter(
  52.             NotFilter::CONNECTION_OR,
  53.             [new EqualsAnyFilter('product.categoriesRo.id', [$hiddenCategoryId])]
  54.         ));
  55.         // $filters = [new ContainsFilter('categoryIds', $hiddenCategoryId),];
  56.         // if ($this->configService->get(ThemeCustomization::HIDE_SUB_CATEGORIES)) {
  57.         //     $context = $event->getContext();
  58.         //     $subCategories = $this->getSubCategories($context, $hiddenCategoryId);
  59.         //     foreach ($subCategories as $subCategory) {
  60.         //         $filters[] = new ContainsFilter('categoryIds', $subCategory['id']);
  61.         //     }
  62.         // }
  63.         // $event->getCriteria()->addFilter(new NotFilter(
  64.         //     NotFilter::CONNECTION_OR,
  65.         //     $filters
  66.         // ));
  67.         // if($isDebug) {
  68.         // }
  69.     }
  70.     private function getSubCategories(Context &$contextstring &$categoryId): array
  71.     {
  72.         $criteria = new Criteria();
  73.         $criteria->addFilter(new EqualsFilter('parentId'$categoryId));
  74.         return $this->categoryRepository->searchIds($criteria$context)->getData();
  75.     }
  76. }