setPriceDefinition example

if (isset($data['description'])) {
            $lineItem->setDescription($data['description']);
        }

        if (isset($data['coverId'])) {
            $cover = $this->mediaRepository->search(new Criteria([$data['coverId']])$context->getContext())->first();

            $lineItem->setCover($cover);
        }

        if (isset($data['priceDefinition'])) {
            $lineItem->setPriceDefinition($this->priceDefinitionFactory->factory($context->getContext()$data['priceDefinition']$data['type']));
        }
    }
}
->setRemovable(true),
            $productIds
        );
        $promotionLineItems = array_map(
            function D$promotionCode) {
                $uniqueKey = 'promotion-' . $promotionCode;

                return (new LineItem($uniqueKey, LineItem::PROMOTION_LINE_ITEM_TYPE))
                    ->setLabel($uniqueKey)
                    ->setGood(false)
                    ->setReferencedId($promotionCode)
                    ->setPriceDefinition(new PercentagePriceDefinition(0));
            },
            $promotionCodes
        );

        $orders = [];

        for ($i = 1; $i <= $numberOfItems; ++$i) {
            $customerId = $context->getFaker()->randomElement($customerIds);

            $salesChannelContext = $this->getContext($customerId$salesChannelIds);

            
private function getDiscountItem(string $promotionId): LineItem
    {
        $discountItemToBeExcluded = new LineItem(Uuid::randomHex(), PromotionProcessor::LINE_ITEM_TYPE);
        $discountItemToBeExcluded->setRequirement(null);
        $discountItemToBeExcluded->setPayloadValue('discountScope', PromotionDiscountEntity::SCOPE_CART);
        $discountItemToBeExcluded->setPayloadValue('discountType', PromotionDiscountEntity::TYPE_ABSOLUTE);
        $discountItemToBeExcluded->setPayloadValue('exclusions', []);
        $discountItemToBeExcluded->setPayloadValue('promotionId', $promotionId);
        $discountItemToBeExcluded->setReferencedId($promotionId);
        $discountItemToBeExcluded->setLabel('PHPUnit');
        $discountItemToBeExcluded->setPriceDefinition(new AbsolutePriceDefinition(-10.0));

        return $discountItemToBeExcluded;
    }
}
ProductCartProcessor::ALLOW_PRODUCT_PRICE_OVERWRITES => true,
            ],
        ];

        $context = $this->getContainer()->get(SalesChannelContextFactory::class)
            ->create($token, TestDefaults::SALES_CHANNEL, $options);

        $definition = new QuantityPriceDefinition(10, new TaxRuleCollection(), 1);

        $product = $this->getContainer()->get(ProductLineItemFactory::class)
            ->create(['id' => $this->ids->get('product'), 'referencedId' => $this->ids->get('product')]$context);
        $product->setPriceDefinition($definition);
        $product->setLabel('My test product');
        $product->setQuantity(5);

        $cart = $this->cartService->getCart($token$context);
        $this->cartService->add($cart$product$context);
        $this->cartService->add($cart$product$context);

        $actualProduct = $cart->get($product->getId());

        static::assertInstanceOf(LineItem::class$product);
        static::assertInstanceOf(LineItem::class$actualProduct);
        
if (isset($data['quantity'])) {
            $lineItem->setQuantity((int) $data['quantity']);
        }

        if (isset($data['priceDefinition']) && !$context->hasPermission(ProductCartProcessor::ALLOW_PRODUCT_PRICE_OVERWRITES)) {
            throw CartException::insufficientPermission();
        }

        if (isset($data['priceDefinition'])) {
            $lineItem->addExtension(ProductCartProcessor::CUSTOM_PRICE, new ArrayEntity());
            $lineItem->setPriceDefinition($this->priceDefinitionFactory->factory($context->getContext()$data['priceDefinition']$data['type']));
        }
    }
}
$product->getRestockTime(),
                    $deliveryTime,
                    $product->getHeight(),
                    $product->getWidth(),
                    $product->getLength()
                )
            );
        }

        // Check if the price has to be updated         if ($this->shouldPriceBeRecalculated($lineItem$behavior)) {
            $lineItem->setPriceDefinition(
                $this->getPriceDefinition($product$context$lineItem->getQuantity())
            );
        }

        $quantityInformation = new QuantityInformation();

        $quantityInformation->setMinPurchase(
            $product->getMinPurchase() ?? 1
        );

        $quantityInformation->setMaxPurchase(
            
/** * @param array<int, int> $creditPrices */
    private function generateCreditItems(Cart $cart, array $creditPrices): Cart
    {
        $lineItems = [];

        foreach ($creditPrices as $price) {
            $creditId = Uuid::randomHex();
            $creditLineItem = (new LineItem($creditId, LineItem::CREDIT_LINE_ITEM_TYPE, $creditId, 1))
                ->setLabel('credit' . $price)
                ->setPriceDefinition(new AbsolutePriceDefinition($price));

            $lineItems[] = $creditLineItem;
        }

        $cart->setRuleIds($this->salesChannelContext->getRuleIds());

        return $this->cartService->add($cart$lineItems$this->salesChannelContext);
    }

    private function createShippingMethod(string $priceRuleId): string
    {
        

    public function surcharge(string $key, string $type, float|PriceCollection $value, string $label): DiscountFacade
    {
        $definition = $this->buildSurchargeDefinition($type$value$key);

        $item = new LineItem($key, LineItem::DISCOUNT_LINE_ITEM, null, 1);
        $item->setGood(false);
        $item->setPriceDefinition($definition);
        $item->setLabel($label);
        $item->setRemovable(true);
        $this->getItems()->add($item);

        return new DiscountFacade($item);
    }

    private function getItems(): LineItemCollection
    {
        return $this->items;
    }

    
$uniqueKey = 'promotion-' . $code;
        $item = new LineItem($uniqueKey, LineItem::PROMOTION_LINE_ITEM_TYPE);
        $item->setLabel($uniqueKey);
        $item->setGood(false);

        // this is used to pass on the code for later usage         $item->setReferencedId($code);

        // this is important to avoid any side effects when calculating the cart         // a percentage of 0,00 will just do nothing         $item->setPriceDefinition(new PercentagePriceDefinition(0));

        return $item;
    }

    public function update(LineItem $lineItem, array $data, SalesChannelContext $context): void
    {
        throw new \RuntimeException(sprintf('You cannot update a line item of type "%s"', $lineItem->getType()));
    }
}


        $this->context = $this->createMock(SalesChannelContext::class);
        $this->context->method('getItemRounding')->willReturn(new CashRoundingConfig(2, 0.01, true));
    }

    public function testCalculateSimplePrice(): void
    {
        $cart = new Cart('test');

        $lineItem = (new LineItem('A', 'test'))
            ->setPriceDefinition(new QuantityPriceDefinition(100, new TaxRuleCollection([])));

        $cart->add($lineItem);

        $calculated = $this->calculator->calculate($cart->getLineItems()$this->context, new CartBehavior());

        static::assertCount(1, $calculated);
        static::assertInstanceOf(LineItem::class$calculated->get('A'));
        static::assertInstanceOf(CalculatedPrice::class$calculated->get('A')->getPrice());
        static::assertSame(100.0, $calculated->get('A')->getPrice()->getTotalPrice());
    }

    

    public function discount(string $key, string $type, float|PriceCollection $value, string $label): DiscountFacade
    {
        $definition = $this->buildDiscountDefinition($type$value$key);

        $item = new LineItem($key, LineItem::DISCOUNT_LINE_ITEM, null, 1);
        $item->setGood(false);
        $item->setRemovable(true);
        $item->setPriceDefinition($definition);
        $item->setLabel($label);
        $item->setRemovable(true);
        $this->getItems()->add($item);

        return new DiscountFacade($item);
    }

    private function getItems(): LineItemCollection
    {
        return $this->items;
    }

    
if (isset($data['description'])) {
            $lineItem->setDescription($data['description']);
        }

        if (isset($data['coverId'])) {
            $cover = $this->mediaRepository->search(new Criteria([$data['coverId']])$context->getContext())->first();

            $lineItem->setCover($cover);
        }

        if (isset($data['priceDefinition'])) {
            $lineItem->setPriceDefinition($this->priceDefinitionFactory->factory($context->getContext()$data['priceDefinition']$data['type']));
        }
    }
}
$this->getContainer()->get('product.repository')
            ->create([$product], Context::createDefaultContext());

        $this->addTaxDataToSalesChannel($this->context, $tax);

        $taxForCustomItem = 20;

        $productLineItem = new LineItem($productId, LineItem::PRODUCT_LINE_ITEM_TYPE, $productId, 1);
        $taxRulesCustomItem = new TaxRuleCollection([new TaxRule($taxForCustomItem)]);
        $customLineItem = (new LineItem($customItemId, LineItem::CUSTOM_LINE_ITEM_TYPE, $customItemId, 1))
            ->setLabel('custom')
            ->setPriceDefinition(new QuantityPriceDefinition(200, $taxRulesCustomItem, 2));

        $creditLineItem = (new LineItem($creditId, LineItem::CREDIT_LINE_ITEM_TYPE, $creditId, 1))
            ->setLabel('credit')
            ->setPriceDefinition(new AbsolutePriceDefinition(-100));

        $cart->addLineItems(new LineItemCollection([$productLineItem$customLineItem$creditLineItem]));

        $calculated = $this->processor->process($cart$this->context, new CartBehavior());

        static::assertCount(3, $calculated->getLineItems());

        
private function createCart(array $items, SalesChannelContext $context): Cart
    {
        $cart = $this->getContainer()
            ->get(CartService::class)
            ->getCart(Uuid::randomHex()$context);

        foreach ($items as $i => $item) {
            $lineItem = new LineItem('item-' . $i, 'test', 'item-' . $i$item->quantity);

            $definition = new QuantityPriceDefinition($item->price, new TaxRuleCollection([new TaxRule($item->taxRate)])$item->quantity);

            $lineItem->setPriceDefinition($definition);

            $price = $this->getContainer()
                ->get(QuantityPriceCalculator::class)
                ->calculate($definition$context);

            $lineItem->setPrice($price);

            $cart->add($lineItem);
        }

        return $cart;
    }
        $uniqueKey = self::PLACEHOLDER_PREFIX . $code;

        $item = new LineItem($uniqueKey, PromotionProcessor::LINE_ITEM_TYPE);
        $item->setLabel($uniqueKey);
        $item->setGood(false);

        // this is used to pass on the code for later usage         $item->setReferencedId($code);

        // this is important to avoid any side effects when calculating the cart         // a percentage of 0,00 will just do nothing         $item->setPriceDefinition(new PercentagePriceDefinition(0));

        return $item;
    }

    /** * Builds a new Line Item for the provided discount and its promotion. * It will automatically reference all provided "product" item Ids within the payload. * * @throws CartException * @throws UnknownPromotionDiscountTypeException */
    
Home | Imprint | This part of the site doesn't use cookies.