custom/plugins/WebLa_HideWithoutStock/src/Subscriber/ProductLoadingSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WebLa_HideWithoutStock\Subscriber;
  4. use WebLa_HideWithoutStock\WebLa_HideWithoutStock;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. class ProductLoadingSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SystemConfigService
  15.      */
  16.     private $systemConfigService;
  17.     public function __construct(SystemConfigService $systemConfigService)
  18.     {
  19.         $this->systemConfigService $systemConfigService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ProductEvents::PRODUCT_SEARCH_CRITERIA => 'onCriteria',
  25.             ProductEvents::PRODUCT_LISTING_CRITERIA => 'onCriteria',
  26.             ProductEvents::PRODUCT_SUGGEST_CRITERIA => 'onCriteria',
  27.         ];
  28.     }
  29.     public function onCriteria($event)
  30.     {
  31.         $salesChannelContext $event->getSalesChannelContext();
  32.         $criteria $event->getCriteria();
  33.         if ($this->systemConfigService->get('WebLa_HideWithoutStock.config.active'$salesChannelContext->getSalesChannel()->getId())) {
  34.             $criteria->addFilter(
  35.                 new MultiFilter(
  36.                     MultiFilter::CONNECTION_OR,
  37.                     [
  38.                         new RangeFilter('availableStock', [RangeFilter::GTE => 1]),
  39.                         new EqualsFilter('customFields.'.WebLa_HideWithoutStock::CUSTOM_FIELD_SHOW_EMPTY_STOCKtrue)
  40.                     ]
  41.                 )
  42.             );
  43.         }
  44.         return $criteria;
  45.     }
  46. }