custom/plugins/MoorlMerchantFinder/src/Storefront/Subscriber/ProductCriteriaSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Moorl\MerchantFinder\Storefront\Subscriber;
  3. use Moorl\MerchantFinder\Core\Content\Merchant\MerchantDefinition;
  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.     public function __construct(SystemConfigService $systemConfigService)
  15.     {
  16.         $this->systemConfigService $systemConfigService;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ProductListingCollectFilterEvent::class => 'onProductListingCollectFilter',
  22.             'sales_channel.product.process.criteria' => 'processCriteria',
  23.         ];
  24.     }
  25.     public function onProductListingCollectFilter(ProductListingCollectFilterEvent $event): void
  26.     {
  27.         if (!$this->systemConfigService->get('MoorlMerchantFinder.config.productFilterMerchant')) {
  28.             return;
  29.         }
  30.         $filters $event->getFilters();
  31.         $request $event->getRequest();
  32.         $ids array_filter(explode('|'$request->query->get('merchant''')));
  33.         $filter = new Filter(
  34.             'merchant',
  35.             !empty($ids),
  36.             [$this->getMerchantEntityAggregation()],
  37.             new EqualsAnyFilter('product.MoorlMerchants.id'$ids),
  38.             $ids
  39.         );
  40.         $filters->add($filter);
  41.     }
  42.     private function getMerchantEntityAggregation(): EntityAggregation
  43.     {
  44.         return new EntityAggregation('merchant''product.MoorlMerchants.id'MerchantDefinition::ENTITY_NAME);
  45.     }
  46.     public function processCriteria(SalesChannelProcessCriteriaEvent $event): void
  47.     {
  48.         $criteria $event->getCriteria();
  49.         $criteria->addAssociation('product.MoorlMerchants');
  50.     }
  51. }