vendor/store.shopware.com/moorlcreator/src/Storefront/Subscriber/ProductCriteriaSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlCreator\Storefront\Subscriber;
  3. use MoorlCreator\Core\Content\Creator\CreatorDefinition;
  4. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  5. use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\System\SalesChannel\Event\SalesChannelProcessCriteriaEvent;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductCriteriaSubscriber implements EventSubscriberInterface
  12. {
  13.     private SystemConfigService $systemConfigService;
  14.     /**
  15.      * ProductCriteriaSubscriber constructor.
  16.      * @param SystemConfigService $systemConfigService
  17.      */
  18.     public function __construct(SystemConfigService $systemConfigService)
  19.     {
  20.         $this->systemConfigService $systemConfigService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             ProductListingCollectFilterEvent::class => 'onProductListingCollectFilter',
  26.             'sales_channel.product.process.criteria' => 'processCriteria',
  27.         ];
  28.     }
  29.     public function onProductListingCollectFilter(ProductListingCollectFilterEvent $event): void
  30.     {
  31.         if (!$this->systemConfigService->get('MoorlCreator.config.enableListingFilter')) {
  32.             return;
  33.         }
  34.         $filters $event->getFilters();
  35.         $request $event->getRequest();
  36.         $ids array_filter(explode('|'$request->query->get('creator''')));
  37.         $filter = new Filter(
  38.             'creator',
  39.             !empty($ids),
  40.             [$this->getCreatorEntityAggregation()],
  41.             new EqualsAnyFilter('product.creators.id'$ids),
  42.             $ids
  43.         );
  44.         $filters->add($filter);
  45.     }
  46.     private function getCreatorEntityAggregation(): EntityAggregation
  47.     {
  48.         return new EntityAggregation('creator''product.creators.id'CreatorDefinition::ENTITY_NAME);
  49.     }
  50.     public function processCriteria(SalesChannelProcessCriteriaEvent $event): void
  51.     {
  52.         $criteria $event->getCriteria();
  53.         $criteria->addAssociation('creators.avatar');
  54.     }
  55. }