LineItemQuantityCollection example



        return $affectedPrices;
    }

    /** * Gets a list of all line item entries * that existing within all packages. */
    public function getAllLineMetaItems(): LineItemQuantityCollection
    {
        $items = new LineItemQuantityCollection();

        /** @var DiscountPackage $package */
        foreach ($this->elements as $package) {
            foreach ($package->getMetaData() as $item) {
                $items->add($item);
            }
        }

        $items->compress();

        return $items;
    }
$definition = $this->getGroupDefinition($groupId$groups);

        $result = $this->groupBuilder->findGroupPackages([$definition]$cart$context);

        $units = [];

        $addedGroupCount = 0;
        foreach ($result->getGroupResult($definition) as $group) {
            $singleItems = $this->splitQuantities($group->getItems());

            $units[] = new DiscountPackage(new LineItemQuantityCollection($singleItems));

            ++$addedGroupCount;

            if ($addedGroupCount === $maximumSetCount) {
                break;
            }
        }

        return new DiscountPackageCollection($units);
    }

    
$discountedItems = 0;

        foreach ($sortedPackages as $package) {
            // filter and collect the items of our             // current package and add the matching items to our list             $items = $this->collectPackageItems($package$applierIndexes);

            // now iterate through all items             // and make sure we only add the collected items             // up to a maximum count depending on our settings             $addItems = new LineItemQuantityCollection();

            foreach ($items as $item) {
                if ($maxItems > self::UNLIMITED && $discountedItems >= $maxItems) {
                    break;
                }

                $addItems->add($item);
                ++$discountedItems;
            }

            if ($addItems->count() <= 0) {
                


                $discountItems[] = $item;
            }
        }

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

        return new DiscountPackage(
            new LineItemQuantityCollection($discountItems)
        );
    }
}
/** * This test verifies that we can add our elements * and that the count returns the correct value * * @group promotions */
    public function testCountReturnsCorrectValue(): void
    {
        $collection = new DiscountPackageCollection(
            [
                new DiscountPackage(new LineItemQuantityCollection()),
                new DiscountPackage(new LineItemQuantityCollection()),
            ]
        );

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

    /** * This test verifies that our object collects the * calculated prices from all existing packages. * * @group promotions */
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));
            }
        }

        return new DiscountPackageCollection($newPackages);
    }

    private function isRulesFilterValid(LineItem $item, PriceDefinitionInterface $priceDefinition, SalesChannelContext $context): bool
    {
        // if the price definition doesnt allow filters,         // then return valid for the item         if (!method_exists($priceDefinition, 'getFilter')) {
            
#[Package('buyers-experience')] abstract class AbstractPriceSorter implements FilterSorterInterface
{
    public function sort(DiscountPackageCollection $packages): DiscountPackageCollection
    {
        foreach ($packages as $package) {
            /** @var array<LineItemQuantity> $metaItems */
            $metaItems = $package->getMetaData()->getElements();

            $metaItems = $this->_sort($metaItems$package);

            $package->setMetaItems(new LineItemQuantityCollection($metaItems));
        }

        return $packages;
    }

    /** * @param array<string, LineItemQuantity[]> $map * * @return array<string, LineItemQuantity[]> */
    abstract protected function sortPriceMap(array $map): array;

    


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

    

        );

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

        

    use PromotionLineItemTestFixtureBehaviour;

    /** * This test verifies that we have an empty and valid * list for new objects. * * @group promotions */
    public function testMetaDataItemsEmptyOnNewObject(): void
    {
        $package = new DiscountPackage(new LineItemQuantityCollection());

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

    /** * This test verifies that we correctly assign the * provided list of our line item quantity items and * return it in the getter. * * @group promotions */
    
return 'HORIZONTAL';
    }

    /** * This picker returns a single package with all items * within this package. * So all packages are moved to a single package. And all * items are considered as if they would be in a single unit. */
    public function pickItems(DiscountPackageCollection $units): DiscountPackageCollection
    {
        $items = new LineItemQuantityCollection();

        foreach ($units as $unit) {
            foreach ($unit->getMetaData() as $item) {
                $items->add($item);
            }
        }

        $package = new DiscountPackage($items);

        return new DiscountPackageCollection([$package]);
    }
}
/** * 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 */
class FilterSorterPriceTest extends TestCase
{
    /** * @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
    {
// now run through all definitions             // and check if our set and count is valid             foreach ($definitions as $definition) {
                $groupResult = $result->getGroupResult($definition);

                $itemsInGroup = $groupResult[$i]->getItems();

                $itemsInSet = array_merge($itemsInSet$itemsInGroup);
            }

            $units[] = new DiscountPackage(new LineItemQuantityCollection($itemsInSet));
        }

        $splitUnits = [];

        foreach ($units as $group) {
            $singleItems = $this->splitQuantities($group->getMetaData()->getElements());

            $splitUnits[] = new DiscountPackage(new LineItemQuantityCollection($singleItems));
        }

        return new DiscountPackageCollection($splitUnits);
    }
Home | Imprint | This part of the site doesn't use cookies.