custom/plugins/CustomPrice/src/Subscriber/ProductSubscriber.php line 25

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TigerMedia\CustomPrice\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  4. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  5. use Shopware\Core\Content\Product\ProductEvents;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  9. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             ProductEvents::PRODUCT_LISTING_CRITERIA => ['filterPrices'10000],
  17.             ProductEvents::PRODUCT_SEARCH_CRITERIA  => 'filterPrices',
  18.             ProductPageCriteriaEvent::class         => 'filterPrices'
  19.         ];
  20.     }
  21.     public function filterPrices(ProductListingCriteriaEvent|ProductPageCriteriaEvent|ProductSearchCriteriaEvent $event): void
  22.     {
  23.         if ($event->getSalesChannelContext()->getCurrencyId() !== Defaults::CURRENCY) {
  24.             $event->getCriteria()
  25.                 ->addAssociation('custom_prices')
  26.                 ->addFilter(new OrFilter(
  27.                     [
  28.                         new RangeFilter('price', [RangeFilter::GT => 0]),
  29.                         new RangeFilter('custom_prices.price', [RangeFilter::GT => 0])
  30.                     ]
  31.                 ));
  32.         }
  33.     }
  34. }