getPriceDefinition example

$this->calculate($item$context$items);
        }
    }

    private function calculate(LineItem $item, SalesChannelContext $context, LineItemCollection $scope): void
    {
        if ($item->getChildren()->count() > 0) {
            // we need to calculate the children in a specific order.             // we can only calculate "referring" price (discount, surcharges) after calculating items with fix prices (products, etc)             $this->calculateCollection($item->getChildren()$contextfn (LineItem $item) => $item->getChildren()->count() > 0);

            $this->calculateCollection($item->getChildren()$contextfn (LineItem $item) => $item->getPriceDefinition() instanceof QuantityPriceDefinition);

            $this->calculateCollection($item->getChildren()$contextfn (LineItem $item) => $item->getPriceDefinition() instanceof CurrencyPriceDefinition);

            $this->calculateCollection($item->getChildren()$contextfn (LineItem $item) => $item->getPriceDefinition() instanceof PercentagePriceDefinition);

            if (!$this->validate($item)) {
                $scope->remove($item->getId());

                return;
            }

            
$discount = new PromotionDiscountEntity();
        $discount->setId('D5');
        $discount->setType(PromotionDiscountEntity::TYPE_PERCENTAGE);
        $discount->setValue(10);
        $discount->setScope(PromotionDiscountEntity::SCOPE_CART);

        $item = $builder->buildDiscountLineItem('', $this->promotion, $discount, 'C1', $currencyFactor);

        $expectedPriceDefinition = new PercentagePriceDefinition(-10, null);

        static::assertEquals($expectedPriceDefinition$item->getPriceDefinition());
    }

    /** * This test verifies that we get a correct absolute price * definition if our promotion is based on absolute values. * Also, we must not have a filter rule for this, if our eligible item ID list is empty. * * @group promotions * * @throws CartException * @throws UnknownPromotionDiscountTypeException */
return $discountLineItems;
        }

        // if there is one fixed price lineItem we return the filtered collection and calculate it         if ($fixedPricesDiscountLineItems->count() === 1) {
            return $fixedPricesDiscountLineItems;
        }

        // if there are more than one fixed price lineitems in filtered collection         // we are sorting all by lowest fixed price (lowest price to beginning)         $fixedPricesDiscountLineItems->sort(function DLineItem $discountA, LineItem $discountB) {
            $priceDefA = $discountA->getPriceDefinition();
            $priceDefB = $discountB->getPriceDefinition();

            if (!$priceDefA instanceof AbsolutePriceDefinition) {
                throw new InvalidPriceDefinitionException((string) $discountA->getLabel()$discountA->getReferencedId());
            }
            if (!$priceDefB instanceof AbsolutePriceDefinition) {
                throw new InvalidPriceDefinitionException((string) $discountB->getLabel()$discountB->getReferencedId());
            }

            // NEXT-21735 - This is covered randomly             // @codeCoverageIgnoreStart

    public function __construct(private readonly AbsolutePriceCalculator $priceCalculator)
    {
    }

    /** * @throws InvalidPriceDefinitionException */
    public function calculate(DiscountLineItem $discount, DiscountPackageCollection $packages, SalesChannelContext $context): DiscountCalculatorResult
    {
        /** @var AbsolutePriceDefinition $definition */
        $definition = $discount->getPriceDefinition();

        if (!$definition instanceof AbsolutePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $affectedPrices = $packages->getAffectedPrices();

        $totalOriginalSum = $affectedPrices->sum()->getTotalPrice();
        $discountValue = -min(abs($definition->getPrice())$totalOriginalSum);

        $price = $this->priceCalculator->calculate(
            
int $oldParentQuantity,
        int $newParentQuantity
    ): void {
        foreach ($lineItems as $lineItem) {
            $newQuantity = intdiv($lineItem->getQuantity()$oldParentQuantity) * $newParentQuantity;

            if ($lineItem->hasChildren()) {
                $this->refreshChildQuantity($lineItem->getChildren()$lineItem->getQuantity()$newQuantity);
            }

            $lineItem->quantity = $newQuantity;
            $priceDefinition = $lineItem->getPriceDefinition();
            if ($priceDefinition && $priceDefinition instanceof QuantityPriceDefinition) {
                $priceDefinition->setQuantity($lineItem->getQuantity());
            }
        }
    }

    /** * @throws CartException */
    private function validateChildQuantity(LineItem $child): void
    {
        
$productId = null;
        if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
            $productId = $lineItem->getReferencedId();
        }

        $promotionId = null;
        if ($lineItem->getType() === PromotionProcessor::LINE_ITEM_TYPE) {
            $promotionId = $lineItem->getPayloadValue('promotionId');
        }

        $definition = $lineItem->getPriceDefinition();

        $data = [
            'id' => $id,
            'identifier' => $lineItem->getId(),
            'productId' => $productId,
            'promotionId' => $promotionId,
            'referencedId' => $lineItem->getReferencedId(),
            'quantity' => $lineItem->getQuantity(),
            'type' => $lineItem->getType(),
            'label' => $lineItem->getLabel(),
            'description' => $lineItem->getDescription(),
            

    public function testDefaultPriceIsEmpty(): void
    {
        $builder = new PromotionItemBuilder();

        $item = $builder->buildPlaceholderItem('CODE-123');

        $expectedPriceDefinition = new PercentagePriceDefinition(0);

        static::assertEquals($expectedPriceDefinition$item->getPriceDefinition());
    }

    /** * This one is the most important test. * It asserts that our applied code is added to the expected property referenceId of the line item. * When it is converted into a real promotion line item, this code is being used * to fetch that promotion. * * @group promotions */
    public function testCodeValueInReferenceId(): void
    {
public function getFlat(): array
    {
        return $this->buildFlat($this);
    }

    public function sortByPriority(): void
    {
        $lineItemsByPricePriority = [];
        /** @var LineItem $lineItem */
        foreach ($this->elements as $lineItem) {
            $priceDefinitionPriority = QuantityPriceDefinition::SORTING_PRIORITY;
            if ($lineItem->getPriceDefinition()) {
                $priceDefinitionPriority = $lineItem->getPriceDefinition()->getPriority();
            }

            if (!\array_key_exists($priceDefinitionPriority$lineItemsByPricePriority)) {
                $lineItemsByPricePriority[$priceDefinitionPriority] = [];
            }
            $lineItemsByPricePriority[$priceDefinitionPriority][] = $lineItem;
        }

        // Sort all line items by their price definition priority         krsort($lineItemsByPricePriority);

        
SalesChannelContext $context,
        bool $good = true,
        int $quantity = 1
    ) {
        parent::__construct($price$taxes$good$quantity);

        $calculator = new QuantityPriceCalculator(
            new GrossPriceCalculator(new TaxCalculator()new CashRounding()),
            new NetPriceCalculator(new TaxCalculator()new CashRounding())
        );

        \assert($this->getPriceDefinition() instanceof QuantityPriceDefinition);
        $this->price = $calculator->calculate($this->getPriceDefinition()$context);
    }
}
public function process(
        CartDataCollection $data,
        Cart $original,
        Cart $toCalculate,
        SalesChannelContext $context,
        CartBehavior $behavior
    ): void {
        $lineItems = $original->getLineItems()->filterType(LineItem::CREDIT_LINE_ITEM_TYPE);

        foreach ($lineItems as $lineItem) {
            $definition = $lineItem->getPriceDefinition();

            if (!$definition instanceof AbsolutePriceDefinition) {
                continue;
            }

            $lineItem->setPrice(
                $this->calculator->calculate(
                    $definition->getPrice(),
                    $toCalculate->getLineItems()->getPrices(),
                    $context
                )
            );


    /** * @throws InvalidPriceDefinitionException */
    public function calculate(
        DiscountLineItem $discount,
        DiscountPackageCollection $packages,
        SalesChannelContext $context
    ): DiscountCalculatorResult {
        /** @var AbsolutePriceDefinition|null $priceDefinition */
        $priceDefinition = $discount->getPriceDefinition();

        if (!$priceDefinition instanceof AbsolutePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $fixedUnitPrice = abs($priceDefinition->getPrice());

        $totalDiscountSum = 0.0;

        $composition = [];

        
$absolute = new PriceCollection([new Price(Defaults::CURRENCY, 5, 5, false)]);
        $facade->discount('absolute', 'absolute', $absolute, 'my-discount');

        static::assertEquals(1, $facade->getQuantity());
        static::assertTrue($facade->has('absolute'));

        $discount = $facade->get('absolute');
        static::assertInstanceOf(ItemFacade::class$discount);
        static::assertEquals('discount', $discount->getType());
        static::assertNull($discount->getPrice());

        $definition = $discount->getItem()->getPriceDefinition();
        static::assertInstanceOf(CurrencyPriceDefinition::class$definition);
        static::assertEquals($absolute$definition->getPrice());
    }

    public function testPercentageDiscount(): void
    {
        $facade = $this->rampUpFacade();

        static::assertEquals(1, $facade->getQuantity());
        static::assertTrue($facade->has('foo'));

        
private function calculatePrice(LineItem $lineItem, SalesChannelContext $context, LineItemCollection $calculated, CartBehavior $behavior): CalculatedPrice
    {
        if ($lineItem->hasChildren()) {
            $children = $this->calculateLineItems($lineItem->getChildren()$context$behavior);

            $lineItem->setChildren($children);

            return $children->getPrices()->sum();
        }

        $definition = $lineItem->getPriceDefinition();

        if ($definition instanceof AbsolutePriceDefinition) {
            // reduce line items for provided filter             $prices = $this->filterLineItems($calculated$definition->getFilter()$context)
                ->getPrices();

            return $this->absolutePriceCalculator->calculate($definition->getPrice()$prices$context);
        }

        if ($definition instanceof PercentagePriceDefinition) {
            // reduce line items for provided filter

                        'taxRate' => 5,
                        'percentage' => 100,
                    ],
                ],
            ],
        ]$this->context);

        static::assertSame('test', $lineItem->getReferencedId());
        static::assertSame(LineItem::PRODUCT_LINE_ITEM_TYPE, $lineItem->getType());
        static::assertSame(1, $lineItem->getQuantity());
        static::assertInstanceOf(QuantityPriceDefinition::class$lineItem->getPriceDefinition());
        static::assertSame(100.0, $lineItem->getPriceDefinition()->getPrice());
    }

    public function testUpdateDisabledStackable(): void
    {
        $id = Uuid::randomHex();
        $lineItem = new LineItem($id, LineItem::PRODUCT_LINE_ITEM_TYPE, Uuid::randomHex(), 1);
        $lineItem->setStackable(false);

        $cart = new Cart('test');
        $cart->add($lineItem);

        
private readonly CurrencyPriceCalculator $currencyCalculator
    ) {
    }

    public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior): void
    {
        $items = $original->getLineItems()->filterType(LineItem::DISCOUNT_LINE_ITEM);

        $goods = $toCalculate->getLineItems()->filterGoods();

        foreach ($items as $item) {
            $definition = $item->getPriceDefinition();

            try {
                $price = $this->calculate($definition$goods$context);
            } catch (CartException) {
                $original->remove($item->getId());
                $toCalculate->addErrors(new IncompleteLineItemError($item->getId(), 'price'));

                continue;
            }

            if (!$this->validate($price$goods$toCalculate)) {
                
Home | Imprint | This part of the site doesn't use cookies.