custom/plugins/WbfkExtensions/src/Subscriber/ProductListingSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace WbfkExtensions\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  4. use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ProductListingSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             ProductListingCollectFilterEvent::class => 'addFilter',
  15.         ];
  16.     }
  17.     public function addFilter(ProductListingCollectFilterEvent $event): void
  18.     {
  19.         $filters $event->getFilters();
  20.         $request $event->getRequest();
  21.         $filtered = (bool)$request->get('has-additional-services');
  22.         $filter = new Filter(
  23.             'hasAdditionalServices',
  24.             $filtered,
  25.             [
  26.                 new FilterAggregation(
  27.                     'has-additional-services',
  28.                     new MaxAggregation('has-additional-services''product.additionalService.active'),
  29.                     [new EqualsFilter('product.additionalService.active'true)]
  30.                 ),
  31.             ],
  32.             new EqualsFilter('product.additionalService.active'true),
  33.             $filtered
  34.         );
  35.         $filters->add($filter);
  36.     }
  37. }