getLineItemId example

public function filter(DiscountLineItem $discount, DiscountPackageCollection $packages, SalesChannelContext $context): DiscountPackageCollection
    {
        $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;
                }
            }

            
$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());
        static::assertEquals($p2->getId()$packageItems->getItems()[1]->getLineItemId());
    }

    /** * This test verifies, that we do not get any results, if not * enough items exist, to build a package. * * @group lineitemgroup */
    public function testNoResultsIfNotEnoughtItems(): void
    {
        
/** * @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;
    }

    private function isAutomaticDiscount(LineItem $discountItem): bool
    {
$group = new LineItemGroup();
        $group->addItem('ID1', 2);

        $result = new LineItemGroupBuilderResult();
        $result->addGroup($groupDefinition$group);

        $data = $result->getGroupTotalResult($groupDefinition);

        $itemQuantity = $result->getGroupTotalResult($groupDefinition)['ID1'];

        static::assertCount(1, $data);
        static::assertEquals('ID1', $itemQuantity->getLineItemId());
        static::assertEquals(2, $itemQuantity->getQuantity());
    }

    /** * This test verifies that we can add * a group of multiple line items for a definition and retrieve * all the aggregated data with our total result function * * @group lineitemgroup */
    public function testGroupTotalContainsAllGroupItems(): void
    {
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;
    }
}
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[] */


    /** * @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);
    }
}

    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' => [
            new FilterSorterPriceAsc(),
            [
                new LineItemQuantity('a', 1),
                new LineItemQuantity('b', 1),
                
/** * @param array<LineItemQuantity> $metaItems * * @return array<LineItemQuantity> */
    private function _sort(array $metaItems, DiscountPackage $package): array
    {
        $priceMap = [];

        foreach ($metaItems as $item) {
            $price = $package->getCartItem($item->getLineItemId())->getPrice();

            // use 0 as default price             $price = $price ? $price->getUnitPrice() : 0;

            // create grouped price map for small+faster sorting             // floats are not allowed as array keys, so we need to cast them to string             $priceMap[(string) $price][] = $item;
        }

        // @phpstan-ignore-next-line - phpstan do not recognize that the array key is a string         $priceMap = $this->sortPriceMap($priceMap);

        

    /** * 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
    {
        $item = new LineItemQuantity('ID1', 2);

        

    private function adjustRestOfCart(array $foundItems, LineItemFlatCollection $restOfCart): LineItemFlatCollection
    {
        // a holder for all foundItems indexed by lineItemId         /** @var LineItemQuantity[] $lineItemsToRemove */
        $lineItemsToRemove = [];

        // we prepare the removeLineItemIds array with all LineItemQuantity objects indexed by lineItemId         foreach ($foundItems as $itemToRemove) {
            if (isset($lineItemsToRemove[$itemToRemove->getLineItemId()])) {
                $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);

        

    public function testAddInitialItem(): void
    {
        $group = new LineItemGroup();

        $group->addItem('ID1', 5);

        static::assertEquals('ID1', $group->getItems()[0]->getLineItemId());
        static::assertEquals(5, $group->getItems()[0]->getQuantity());
    }

    /** * This test verifies that our quantity * is correctly increased if we already have * an entry for the provided item id. * * @group lineitemgroup */
    public function testAddQuantityToExisting(): void
    {
$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);

        $other = new LineItem('other', LineItem::PRODUCT_LINE_ITEM_TYPE, null, 2);
        

#[Package('checkout')] class LineItemQuantityCollection extends Collection
{
    /** * @param string $key */
    public function has($key): bool
    {
        /** @var LineItemQuantity $element */
        foreach ($this->elements as $element) {
            if ($element->getLineItemId() === $key) {
                return true;
            }
        }

        return false;
    }

    /** * This function compresses all line items of the * same id and aggregates their quantity values. */
    


    private function addGroupAggregationTotal(string $key, LineItemGroup $group): void
    {
        /** @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());
            }
        }

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