vendor/shopware/core/Checkout/Cart/LineItem/Group/RulesMatcher/AnyRuleMatcher.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\LineItem\Group\RulesMatcher;
  3. use Shopware\Core\Checkout\Cart\LineItem\Group\LineItemGroupDefinition;
  4. use Shopware\Core\Checkout\Cart\LineItem\Group\LineItemGroupRuleMatcherInterface;
  5. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  6. use Shopware\Core\Checkout\Cart\LineItem\LineItemFlatCollection;
  7. use Shopware\Core\Checkout\Cart\Rule\LineItemScope;
  8. use Shopware\Core\Framework\Rule\Rule;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. class AnyRuleMatcher implements LineItemGroupRuleMatcherInterface
  11. {
  12.     public function getMatchingItems(
  13.         LineItemGroupDefinition $groupDefinition,
  14.         LineItemFlatCollection $items,
  15.         SalesChannelContext $context
  16.     ): LineItemFlatCollection {
  17.         $matchingItems = [];
  18.         foreach ($items as $item) {
  19.             if ($this->isAnyRuleMatching($groupDefinition$item$context)) {
  20.                 $matchingItems[] = $item;
  21.             }
  22.         }
  23.         return new LineItemFlatCollection($matchingItems);
  24.     }
  25.     /**
  26.      * Gets if the provided line item is allowed for any of the applied
  27.      * rules within the group entity.
  28.      */
  29.     private function isAnyRuleMatching(
  30.         LineItemGroupDefinition $groupDefinition,
  31.         LineItem $item,
  32.         SalesChannelContext $context
  33.     ): bool {
  34.         // no rules mean OK
  35.         if ($groupDefinition->getRules()->count() <= 0) {
  36.             return true;
  37.         }
  38.         // if we have rules, make sure
  39.         // they are connected using an OR condition
  40.         $scope = new LineItemScope($item$context);
  41.         foreach ($groupDefinition->getRules() as $rule) {
  42.             $rootCondition $rule->getPayload();
  43.             // if any rule matches, return OK
  44.             if ($rootCondition->match($scope)) {
  45.                 return true;
  46.             }
  47.         }
  48.         return false;
  49.     }
  50. }