<?php
namespace WbfkExtensions\Subscriber;
use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductListingSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductListingCollectFilterEvent::class => 'addFilter',
];
}
public function addFilter(ProductListingCollectFilterEvent $event): void
{
$filters = $event->getFilters();
$request = $event->getRequest();
$filtered = (bool)$request->get('has-additional-services');
$filter = new Filter(
'hasAdditionalServices',
$filtered,
[
new FilterAggregation(
'has-additional-services',
new MaxAggregation('has-additional-services', 'product.additionalService.active'),
[new EqualsFilter('product.additionalService.active', true)]
),
],
new EqualsFilter('product.additionalService.active', true),
$filtered
);
$filters->add($filter);
}
}