<?php declare(strict_types=1);
namespace TigerMedia\CustomPrice\Subscriber;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_LISTING_CRITERIA => ['filterPrices', 10000],
ProductEvents::PRODUCT_SEARCH_CRITERIA => 'filterPrices',
ProductPageCriteriaEvent::class => 'filterPrices'
];
}
public function filterPrices(ProductListingCriteriaEvent|ProductPageCriteriaEvent|ProductSearchCriteriaEvent $event): void
{
if ($event->getSalesChannelContext()->getCurrencyId() !== Defaults::CURRENCY) {
$event->getCriteria()
->addAssociation('custom_prices')
->addFilter(new OrFilter(
[
new RangeFilter('price', [RangeFilter::GT => 0]),
new RangeFilter('custom_prices.price', [RangeFilter::GT => 0])
]
));
}
}
}