LineItemFlatCollection example


    public function testAffectedPricesFromAllPackages(): void
    {
        $product1 = $this->createProductItem(29, 19);
        $product2 = $this->createProductItem(14, 19);

        $package1 = new DiscountPackage(new LineItemQuantityCollection());
        $package1->setCartItems(new LineItemFlatCollection([$product1]));

        $package2 = new DiscountPackage(new LineItemQuantityCollection());
        $package2->setCartItems(new LineItemFlatCollection([$product2]));

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

        static::assertEquals(2, $collection->getAffectedPrices()->count());
    }

    /** * This test verifies that our object collects the * line items from all existing packages. * * @group promotions */
/** * This test verifies that we have finished building our * package, as soon as we have reached a gross item price of 100.0. * This is archived with 2 items - but most important - only if taxes are considered. * Otherwise it would be 3 items. * * @group lineitemgroup */
    public function testPackageDoneWhenSumReached(): void
    {
        $items = new LineItemFlatCollection();
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));

        $packageItems = $this->packager->buildGroupPackage(100, $items$this->context);

        // verify we have 2 items, then we have reached 100.0
if ($a->getPrice() === null) {
                return 1;
            }

            if ($b->getPrice() === null) {
                return 0;
            }

            return $b->getPrice()->getUnitPrice() <=> $a->getPrice()->getUnitPrice();
        });

        return new LineItemFlatCollection($sorted);
    }
}

    public function testSortPriceDESC(): void
    {
        $p1 = $this->createProductItem(50.0, 0);
        $p2 = $this->createProductItem(23.5, 0);
        $p3 = $this->createProductItem(150.0, 0);

        $items = new LineItemFlatCollection();
        $items->add($p1);
        $items->add($p2);
        $items->add($p3);

        $sortedItems = $this->sorter->sort($items);

        static::assertEquals($p3->getId()$sortedItems->getElements()[0]->getId());
        static::assertEquals($p1->getId()$sortedItems->getElements()[1]->getId());
        static::assertEquals($p2->getId()$sortedItems->getElements()[2]->getId());
    }

    
LineItemFlatCollection $items,
        SalesChannelContext $context
    ): LineItemFlatCollection {
        $matchingItems = [];

        foreach ($items as $item) {
            if ($this->anyRuleProvider->isMatching($groupDefinition$item$context)) {
                $matchingItems[] = $item;
            }
        }

        return new LineItemFlatCollection($matchingItems);
    }
}

    public function testCanAddSameItemMultipleTimes(): void
    {
        $lineItem = new LineItem('ABC', '');
        $lineItem->setStackable(true);

        $collection = new LineItemFlatCollection();

        $collection->add($lineItem);
        $collection->add($lineItem);

        static::assertCount(2, $collection);
    }
}
$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
    {
        $context = Generator::createSalesChannelContext();

        $rounding = new CashRounding();

        
$quantity = $lineItemsToRemove[$itemToRemove->getLineItemId()];
                $lineItemsToRemove[$itemToRemove->getLineItemId()]->setQuantity($quantity->getQuantity() + $itemToRemove->getQuantity());

                continue;
            }
            $lineItemsToRemove[$itemToRemove->getLineItemId()] = $itemToRemove;
        }

        /** @var array<string> $lineItemsToRemoveIDs */
        $lineItemsToRemoveIDs = array_keys($lineItemsToRemove);

        $newRestOfCart = new LineItemFlatCollection();

        // this is our running buffer         // for the items that need to be removed         $deleteBuffer = [];

        // make sure we have an ID index for         // all our delete-items with a qty of 0         foreach (array_keys($lineItemsToRemove) as $id) {
            $deleteBuffer[$id] = 0;
        }

        
if ($a->getPrice() === null) {
                return 0;
            }

            if ($b->getPrice() === null) {
                return 1;
            }

            return $a->getPrice()->getUnitPrice() <=> $b->getPrice()->getUnitPrice();
        });

        return new LineItemFlatCollection($sorted);
    }
}
static::assertEquals(0, $package->getCartItems()->count());
    }

    /** * This test verifies that we correctly assign the * provided list of our cart items and return it in the getter. * * @group promotions */
    public function testCartItemsAreCorrectlyAdded(): void
    {
        $cartItems = new LineItemFlatCollection();
        $product = $this->createProductItem(29, 19);
        $cartItems->add($product);

        $package = new DiscountPackage(new LineItemQuantityCollection());
        $package->setCartItems($cartItems);

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

    /** * This test verifies that we dont get an exception * when requesting the price without any items. * We have to get 0,00 in this case. * * @group promotions */
// 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->setPrice($amount);
    }

    /** * @throws CartException */
    private function enrichPackagesWithCartData(DiscountPackageCollection $result, Cart $cart, SalesChannelContext $context): DiscountPackageCollection
    {
        // set the line item from the cart for each unit         foreach ($result as $package) {
            $cartItemsForUnit = new LineItemFlatCollection();

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

                $cartItemsForUnit->add($this->splitted[$lineItemId]);
            }

            $package->setCartItems($cartItemsForUnit);
        }

        return $result;
    }
class DiscountPackage
{
    private LineItemFlatCollection $cartItems;

    /** * @var array<string, LineItem>|null */
    private ?array $hashMap;

    public function __construct(private LineItemQuantityCollection $metaItems)
    {
        $this->cartItems = new LineItemFlatCollection();
        $this->hashMap = null;
    }

    public function getMetaData(): LineItemQuantityCollection
    {
        return $this->metaItems;
    }

    public function setMetaItems(LineItemQuantityCollection $metaItems): void
    {
        $this->metaItems = $metaItems;
    }

    public function testPackageDoneWhenCountReached(): void
    {
        $p1 = $this->createProductItem(50.0, 0);
        $p2 = $this->createProductItem(23.5, 0);
        $p3 = $this->createProductItem(150.0, 0);

        $items = new LineItemFlatCollection();
        $items->add($p1);
        $items->add($p2);
        $items->add($p3);

        $packageItems = $this->packager->buildGroupPackage(2, $items$this->context);

        // verify we have only 2 items         static::assertCount(2, $packageItems->getItems());

        // test that we have the first 2 from our list         static::assertEquals($p1->getId()$packageItems->getItems()[0]->getLineItemId());
        
/** * @dataProvider sortingProvider * * @param array<LineItem> $items * @param array<LineItemQuantity> $meta * @param array<string> $expected */
    public function testSorting(AbstractPriceSorter $sorter, array $meta, array $items, array $expected): void
    {
        $package = new DiscountPackage(new LineItemQuantityCollection($meta));

        $package->setCartItems(new LineItemFlatCollection($items));

        $sorter->sort(new DiscountPackageCollection([$package]));

        $ordered = $package->getMetaData()->fmap(fn (LineItemQuantity $item) => $item->getLineItemId());

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

    public static function sortingProvider(): \Generator
    {
        yield 'Test ascending sorting' => [
            
Home | Imprint | This part of the site doesn't use cookies.