LineItemQuantity example

foreach ($package->getMetaData() as $item) {
            ++$index;

            // if our indexes are empty, then             // we use all items, otherwise do only use             // the items of our pre calculated indexes             if (!empty($applierIndexes) && !\in_array($index$applierIndexes, true)) {
                continue;
            }

            $items->add(new LineItemQuantity($item->getLineItemId()$item->getQuantity()));
        }

        return $items;
    }
}

        /** @var array<mixed> $total */
        $total = $this->results[$key]['total'];

        foreach ($group->getItems() as $tuple) {
            // either create new entries             // or just increase the quantity of an existing entry in             // the result set of our group definition.             if (!\array_key_exists($tuple->getLineItemId()$total)) {
                // add as new entry to avoid pointer references                 // to our single groups list                 $total[$tuple->getLineItemId()] = new LineItemQuantity(
                    $tuple->getLineItemId(),
                    $tuple->getQuantity()
                );
            } else {
                $package = $total[$tuple->getLineItemId()];
                $package->setQuantity($package->getQuantity() + $tuple->getQuantity());
            }
        }

        $this->results[$key]['total'] = $total;
    }
}


        $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);

        $priceDefinition = new PercentagePriceDefinition(42, new LineItemRule(Rule::OPERATOR_EQ, [Uuid::randomHex()]));
        $discount = new DiscountLineItem('foo', $priceDefinition['discountScope' => 'foo', 'discountType' => 'bar'], null);

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

        

        $this->items = [];
    }

    /** * Adds a new data entry for the provided line item id. * It will increase the quantity if already existing. */
    public function addItem(string $lineItemId, int $quantity): void
    {
        if (!\array_key_exists($lineItemId$this->items)) {
            $this->items[$lineItemId] = new LineItemQuantity($lineItemId$quantity);
        } else {
            $package = $this->items[$lineItemId];
            $package->setQuantity($package->getQuantity() + $quantity);
        }
    }

    /** * Gets all added line item quantity packages * that this group contains. * * @return LineItemQuantity[] */
#[Package('checkout')] class LineItemQuantityTest extends TestCase
{
    /** * This test verifies that our property is correctly * assigned and returned in its getter. * * @group lineitemgroup */
    public function testPropertyLineItemId(): void
    {
        $item = new LineItemQuantity('ID1', 2);

        static::assertEquals('ID1', $item->getLineItemId());
    }

    /** * This test verifies that our property is correctly * assigned and returned in its getter. * * @group lineitemgroup */
    public function testPropertQuantity(): void
    {
$discountCalculator = new DiscountAbsoluteCalculator($calculator);

        $priceDefinition = new AbsolutePriceDefinition($discountIn);
        $discount = new DiscountLineItem('foo', $priceDefinition['discountScope' => 'foo', 'discountType' => 'bar'], null);

        $lineItem = new LineItem(Uuid::randomHex(), LineItem::PRODUCT_LINE_ITEM_TYPE, Uuid::randomHex(), 2);
        $lineItem->setPrice(new CalculatedPrice($packageSum / 2, $packageSumnew CalculatedTaxCollection()new TaxRuleCollection()));

        $package = new DiscountPackage(
            new LineItemQuantityCollection([
                new LineItemQuantity($lineItem->getId(), 1),
                new LineItemQuantity($lineItem->getId(), 1),
            ])
        );
        $package->setCartItems(new LineItemFlatCollection([$lineItem]));

        $price = $discountCalculator->calculate($discountnew DiscountPackageCollection([$package])$context);

        static::assertEquals($discountOut$price->getPrice()->getTotalPrice());
    }

    public function testInvalidPriceDefinitionThrow(): void
    {
/** * This test verifies that our object collects the * line items from all existing packages. * * @group promotions */
    public function testAllLineItemsFromAllPackages(): void
    {
        $package1 = new DiscountPackage(new LineItemQuantityCollection(
            [
                new LineItemQuantity('ABC', 2),
            ]
        ));

        $package2 = new DiscountPackage(new LineItemQuantityCollection(
            [
                new LineItemQuantity('DEF', 3),
            ]
        ));

        $collection = new DiscountPackageCollection([$package1$package2]);

        
/** * This test verifies that we correctly assign the * provided list of our line item quantity items and * return it in the getter. * * @group promotions */
    public function testMetaDataItemsAreCorrectlyAdded(): void
    {
        $items = new LineItemQuantityCollection();
        $items->add(new LineItemQuantity('ABC', 2));

        $package = new DiscountPackage(new LineItemQuantityCollection($items));

        static::assertEquals(1, $package->getMetaData()->count());
    }

    /** * This test verifies that we have an empty and valid * list for new objects. * * @group promotions */
$ordered = $package->getMetaData()->fmap(fn (LineItemQuantity $item) => $item->getLineItemId());

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

    public static function sortingProvider(): \Generator
    {
        yield 'Test ascending sorting' => [
            new FilterSorterPriceAsc(),
            [
                new LineItemQuantity('a', 1),
                new LineItemQuantity('b', 1),
                new LineItemQuantity('c', 1),
            ],
            [
                self::item('a', 200),
                self::item('b', 100),
                self::item('c', 300),
            ],
            ['b', 'a', 'c'],
        ];

        


    /** * @param LineItemQuantity[] $groupItems */
    private function splitQuantities(array $groupItems): LineItemQuantityCollection
    {
        $items = [];

        foreach ($groupItems as $item) {
            for ($i = 1; $i <= $item->getQuantity(); ++$i) {
                $items[] = new LineItemQuantity($item->getLineItemId(), 1);
            }
        }

        return new LineItemQuantityCollection($items);
    }
}
throw new SetGroupNotFoundException($groupId);
    }

    /** * @param LineItemQuantity[] $groupItems */
    private function splitQuantities(array $groupItems): LineItemQuantityCollection
    {
        $items = [];

        foreach ($groupItems as $item) {
            $cloneItem = new LineItemQuantity($item->getLineItemId(), 1);
            for ($i = 1; $i <= $item->getQuantity(); ++$i) {
                $items[] = clone $cloneItem;
            }
        }

        return new LineItemQuantityCollection($items);
    }

    /** * Gets a list of in-memory group definitions * from the list of group settings from the payload * * @return LineItemGroupDefinition[] */
return new DiscountPackageCollection([]);
        }

        return new DiscountPackageCollection([$discountPackage]);
    }

    private function getDiscountPackage(LineItemCollection $cartItems): ?DiscountPackage
    {
        $discountItems = [];
        foreach ($cartItems as $cartLineItem) {
            for ($i = 1; $i <= $cartLineItem->getQuantity(); ++$i) {
                $item = new LineItemQuantity(
                    $cartLineItem->getId(),
                    1
                );

                $discountItems[] = $item;
            }
        }

        if (\count($discountItems) === 0) {
            return null;
        }

        
$priceDefinition = $discount->getPriceDefinition();

        $newPackages = [];

        foreach ($packages as $package) {
            $foundItems = [];

            foreach ($package->getMetaData() as $item) {
                $lineItem = $package->getCartItem($item->getLineItemId());

                if ($this->isRulesFilterValid($lineItem$priceDefinition$context)) {
                    $item = new LineItemQuantity(
                        $lineItem->getId(),
                        $lineItem->getQuantity()
                    );

                    $foundItems[] = $item;
                }
            }

            if (\count($foundItems) > 0) {
                $newPackages[] = new DiscountPackage(new LineItemQuantityCollection($foundItems));
            }
        }
class LineItemQuantityCollectionTest extends TestCase
{
    /** * This test verifies that we can correctly * test if our collection has an entry * for the provided item id. * * @group lineitemgroup */
    public function testHasItem(): void
    {
        $item1 = new LineItemQuantity('A', 2);

        $collection = new LineItemQuantityCollection([$item1]);

        static::assertTrue($collection->has('A'));
        static::assertFalse($collection->has('X'));
    }

    /** * This test verifies that we can successfully * compress our list of entries and combine them * into single entries with aggregated quantities. * * @group lineitemgroup */
Home | Imprint | This part of the site doesn't use cookies.