vendor/store.shopware.com/swagdynamicaccess/src/Core/Content/Product/SalesChannel/SalesChannelCriteriaSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\DynamicAccess\Core\Content\Product\SalesChannel;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\Filter;
  10. use Shopware\Core\System\SalesChannel\Event\SalesChannelProcessCriteriaEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class SalesChannelCriteriaSubscriber implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             'sales_channel.product.process.criteria' => 'addProductRuleFilter',
  18.             'sales_channel.category.process.criteria' => 'addCategoryRuleFilter',
  19.             'sales_channel.landing_page.process.criteria' => 'addLandingPageRuleFilter',
  20.         ];
  21.     }
  22.     public function addProductRuleFilter(SalesChannelProcessCriteriaEvent $event): void
  23.     {
  24.         $criteria $event->getCriteria();
  25.         if (!$this->hasFilter($criteriaProductRuleFilter::class)) {
  26.             $criteria->addFilter(new ProductRuleFilter($event->getSalesChannelContext()->getRuleIds()));
  27.         }
  28.     }
  29.     public function addCategoryRuleFilter(SalesChannelProcessCriteriaEvent $event): void
  30.     {
  31.         $criteria $event->getCriteria();
  32.         if (!$this->hasFilter($criteriaCategoryRuleFilter::class)) {
  33.             $criteria->addFilter(new CategoryRuleFilter($event->getSalesChannelContext()->getRuleIds()));
  34.         }
  35.     }
  36.     public function addLandingPageRuleFilter(SalesChannelProcessCriteriaEvent $event): void
  37.     {
  38.         $criteria $event->getCriteria();
  39.         if (!$this->hasFilter($criteriaLandingPageRuleFilter::class)) {
  40.             $criteria->addFilter(new LandingPageRuleFilter($event->getSalesChannelContext()->getRuleIds()));
  41.         }
  42.     }
  43.     /**
  44.      * @param class-string<Filter> $filterClassName
  45.      */
  46.     private function hasFilter(Criteria $criteriastring $filterClassName): bool
  47.     {
  48.         foreach ($criteria->getFilters() as $filter) {
  49.             if ($filter instanceof $filterClassName) {
  50.                 return true;
  51.             }
  52.         }
  53.         return false;
  54.     }
  55. }