getMatchingItems example

// create our group with our price rule         // and use it to match both our products         $group = $this->buildGroup(
            self::KEY_PACKAGER_COUNT,
            1,
            self::KEY_SORTER_PRICE_ASC,
            new RuleCollection([$ruleEntity])
        );

        $matcher = new AnyRuleMatcher(new AnyRuleLineItemMatcher());

        $matchedItems = $matcher->getMatchingItems($groupnew LineItemFlatCollection([$productLower50$product50])$this->context);

        static::assertCount(1, $matchedItems);
        static::assertSame($product50$matchedItems->getElements()[0]);
    }

    /** * This test verifies that our line item matching works correctly with 2 rule combinations. * We create a group with a rule for minimum item price of 50 and minimum quantity of 3. * We have 4 combinations within our products. The multi rules work with a OR condition, so we * should get 3 out of our 4 products that match. Only the product with neither quantity nor price condition * should not match our group rules. * * @group lineitemgroup */
$cart->setLineItems(
            new LineItemCollection([
                $matchingLineItem,
                (new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, Uuid::randomHex()))->setStackable(true),
            ])
        );

        $priceDefinition = new AbsolutePriceDefinition(42, new LineItemRule(Rule::OPERATOR_EQ, [$matchingLineItem->getReferencedId() ?? '']));
        $discount = new DiscountLineItem('foo', $priceDefinition['discountScope' => 'foo', 'discountType' => 'bar'], null);

        $packager = new CartScopeDiscountPackager();
        $items = $packager->getMatchingItems($discount$cart$context);

        $expected = new DiscountPackageCollection([
            new DiscountPackage(
                new LineItemQuantityCollection([
                    new LineItemQuantity($matchingLineItem->getId(), 1),
                    new LineItemQuantity($matchingLineItem->getId(), 1),
                ])
            ),
        ]);

        static::assertEquals($expected$items);

        
$sorter = $this->registry->getSorter($groupDefinition->getSorterKey());
            $packager = $this->registry->getPackager($groupDefinition->getPackagerKey());

            // we have to sort our items first             // otherwise it would be a "random" order when             // adjusting the rest of our cart...             $restOfCart = $sorter->sort($restOfCart);

            // try as long as groups can be             // found for the current definition             while (true) {
                $itemsToConsider = $this->ruleMatcher->getMatchingItems($groupDefinition$restOfCart$context);

                // now build a package with our packager                 $group = $packager->buildGroupPackage($groupDefinition->getValue()$itemsToConsider$context);

                // if we have no found items in our group, quit                 if (!$group->hasItems()) {
                    break;
                }

                // append the currently found group of items                 // to our group definition inside our result object
$item->getPayload(),
            $item->getReferencedId()
        );

        $packager = match ($discount->getScope()) {
            PromotionDiscountEntity::SCOPE_CART => $this->cartScopeDiscountPackager,
            PromotionDiscountEntity::SCOPE_SET => $this->setScopeDiscountPackager,
            PromotionDiscountEntity::SCOPE_SETGROUP => $this->setGroupScopeDiscountPackager,
            default => throw new InvalidScopeDefinitionException($discount->getScope()),
        };

        $packages = $packager->getMatchingItems($discount$calculatedCart$context);

        // check if no result is found,         // then this would mean -> no discount         if ($packages->count() <= 0) {
            return new DiscountCalculatorResult(
                new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection(), 1),
                []
            );
        }

        // remember our initial package count
$packager = $this->getContainer()->get(CartScopeDiscountPackager::class);

        $context = $this->createMock(SalesChannelContext::class);

        $discount = new DiscountLineItem('test', new QuantityPriceDefinition(10, new TaxRuleCollection([]), 1)[
            'discountScope' => 'scope',
            'discountType' => 'type',
            'filter' => [],
        ], null);

        $packages = $packager->getMatchingItems($discount$cart$context);

        $ids = $packages->first()?->getMetaData()->map(fn (LineItemQuantity $item) => $item->getLineItemId());

        static::assertEquals($expected$ids);
    }

    public static function buildPackagesProvider(): \Generator
    {
        $stackable = new LineItem('stackable', LineItem::PRODUCT_LINE_ITEM_TYPE, null, 1);
        $stackable->setPrice(new CalculatedPrice(100, 100, new CalculatedTaxCollection()new TaxRuleCollection()));
        $stackable->setStackable(true);

        
Home | Imprint | This part of the site doesn't use cookies.