DiscountPackage example

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

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

        
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
    {
break;
                }

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

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

            $filteredPackages[] = new DiscountPackage($addItems);
        }

        return new DiscountPackageCollection($filteredPackages);
    }

    private function hasFilterSettings(string $sorterKey, string $applierKey, string $countKey): bool
    {
        if (empty($sorterKey)) {
            return false;
        }

        
new PercentageTaxRuleBuilder()
        );

        $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 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 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 */
1
                );

                $discountItems[] = $item;
            }
        }

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

        return new DiscountPackage(
            new LineItemQuantityCollection($discountItems)
        );
    }
}
$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);
    }

    
// 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);
    }
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')) {
            

    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 */
    

    public function splitPackages(): DiscountPackageCollection
    {
        $tmpPackages = [];

        /** @var DiscountPackage $package */
        foreach ($this->elements as $package) {
            foreach ($package->getMetaData() as $meta) {
                $tmpPackages[] = new DiscountPackage(new LineItemQuantityCollection([$meta]));
            }
        }

        return new self($tmpPackages);
    }

    public function getApiAlias(): string
    {
        return 'promotion_cart_discount_package_collection';
    }

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