custom/plugins/ShortStories/src/Subscriber/ListingSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShortStories\Subscriber;
  4. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;
  12. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;
  13. use Symfony\Component\HttpFoundation\Request;
  14. class ListingSubscriber implements EventSubscriberInterface
  15. {
  16.     // register event
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             ProductListingCollectFilterEvent::class => 'addFilter'
  21.         ];
  22.     }
  23.     public function addFilter(ProductListingCollectFilterEvent $event): void
  24.     {
  25.         // fetch existing filters
  26.         $filters $event->getFilters();
  27.         $request $event->getRequest();
  28.         $ids $this->getPropIds($request"material");
  29.         
  30.         $filter = new Filter(
  31.             // name
  32.             'material',
  33.         
  34.             // filtered
  35.             !empty($ids),
  36.             
  37.             // aggregations
  38.             [
  39.                 new FilterAggregation(
  40.                     'material-filter'
  41.                     new MaxAggregation('material''product.customFields.custom_product_material'),
  42.                     [new EqualsAnyFilter('product.customFields.custom_product_material', ['423f19d967d843bba8166dadfec585a6'] )]
  43.                 ),
  44.             ],
  45.         
  46.             new EqualsAnyFilter('product.customFields.custom_product_material'$ids),
  47.         
  48.             // values
  49.             $ids
  50.         );
  51.         // Add your custom filter
  52.         $filters->add($filter);
  53.     }
  54.     protected function getPropIds(Request $requeststring $prop "tag", ?array $defaultIds null): array
  55.     {
  56.         $ids $request->query->get($prop);
  57.         if ($request->isMethod(Request::METHOD_POST)) {
  58.             $ids $request->request->get($prop);
  59.         }
  60.         if (\is_string($ids)) {
  61.             $ids explode('|'$ids);
  62.         }
  63.         if (empty($ids) && !empty($defaultIds)) {
  64.             $ids $defaultIds;
  65.         }
  66.         return array_filter((array) $ids);
  67.     }
  68.     
  69. }