custom/plugins/LogBchange/src/Subscriber/ProductListingSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace LogBchange\Subscriber;
  3. use LogBchange\Service\HighlightPropertyService;
  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\StatsAggregation;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Shopware\Core\Content\Product\ProductEvents;
  10. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  11. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  12. class ProductListingSubscriber implements EventSubscriberInterface
  13. {
  14.     private HighlightPropertyService $highlightPropertyService;
  15.     public function __construct(HighlightPropertyService $highlightPropertyService)
  16.     {
  17.         $this->highlightPropertyService = $highlightPropertyService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             ProductListingCollectFilterEvent::class => 'addDurationFilter',
  23.             ProductEvents::PRODUCT_LISTING_CRITERIA => 'onProductListingCriteria',
  24.             ProductEvents::PRODUCT_LISTING_RESULT => 'onProductListingResult',
  25.         ];
  26.     }
  27.     public function addDurationFilter(ProductListingCollectFilterEvent $event): void
  28.     {
  29.         // fetch existing filters
  30.         $filters = $event->getFilters();
  31.         $request = $event->getRequest();
  32.         $min = $request->get('min-duration');
  33.         $max = $request->get('max-duration');
  34.         $range = [];
  35.         if ($min !== null && $min >= 0) {
  36.             $range[RangeFilter::GTE] = $min;
  37.         }
  38.         if ($max !== null && $max >= 0) {
  39.             $range[RangeFilter::LTE] = $max;
  40.         }
  41.         $filter = new Filter(
  42.             // unique name of the filter
  43.             'duration',
  44.             // defines if this filter is active
  45.             !empty($range),
  46.             // Defines aggregations behind a filter. A filter can contain multiple aggregations like properties
  47.             [new StatsAggregation('duration', 'product.customFields.custom_product_fields_duration', true, true, false, false)],
  48.             // defines the DAL filter which should be added to the criteria   
  49.             new RangeFilter('product.customFields.custom_product_fields_duration', $range),
  50.             // defines the values which will be added as currentFilter to the result
  51.             [
  52.                 'min' => (float) $request->get('min-duration'),
  53.                 'max' => (float) $request->get('max-duration'),
  54.             ]
  55.         );
  56.         // Add your custom filter
  57.         $filters->add($filter);
  58.     }
  59.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
  60.     {
  61.         $event->getCriteria()->addAssociation('properties.media');
  62.         $event->getCriteria()->addAssociation('properties.group');
  63.     }
  64.     public function onProductListingResult(ProductListingResultEvent $event): void
  65.     {
  66.         foreach ($event->getResult()->getEntities() as $product) {
  67.             $this->highlightPropertyService->enrichHighlightProperties($product);
  68.         }
  69.     }
  70. }