setPrice example

$lineItem = new LineItem(Uuid::randomHex(), 'product');
        $lineItem->setDeliveryInformation(
            new DeliveryInformation(
                10,
                12.0,
                false,
                null,
                $this->deliveryTime
            )
        );
        $lineItem->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection()));
        $price = $lineItem->getPrice();
        static::assertNotNull($price);

        $delivery->expects(static::once())->method('getPositions')->willReturn(
            new DeliveryPositionCollection(
                [
                    new DeliveryPosition(
                        Uuid::randomHex(),
                        $lineItem,
                        1,
                        $price,
                        

    }

    private function calculateAmount(SalesChannelContext $context, Cart $cart): void
    {
        $amount = $this->amountCalculator->calculate(
            $cart->getLineItems()->getPrices(),
            $cart->getDeliveries()->getShippingCosts(),
            $context
        );

        $cart->setPrice($amount);
    }
}
/** * This test verifies, that our packager does ignore products * that have no calculated price. * * @group lineitemgroup */
    public function testPriceNullIsIgnored(): void
    {
        $items = new LineItemFlatCollection();

        $productNoPrice = $this->createProductItem(20.0, 19);
        $productNoPrice->setPrice(null);

        $items->add($productNoPrice);
        $items->add($this->createProductItem(20.0, 19));

        $packageItems = $this->packager->buildGroupPackage(5, $items$this->context);

        static::assertCount(1, $packageItems->getItems());
    }
}
static::assertNotSame(
            $collection->filterGoods(),
            $collection->filterGoods()
        );
    }

    public function testGetPricesCollectionOfMultipleItems(): void
    {
        $lineItems = new LineItemCollection([
            (new LineItem('A', 'test'))
                ->setPrice(new CalculatedPrice(200, 200, new CalculatedTaxCollection()new TaxRuleCollection())),

            (new LineItem('B', 'test'))
                ->setPrice(new CalculatedPrice(300, 300, new CalculatedTaxCollection()new TaxRuleCollection())),
        ]);

        static::assertEquals(
            new PriceCollection([
                new CalculatedPrice(200, 200, new CalculatedTaxCollection()new TaxRuleCollection()),
                new CalculatedPrice(300, 300, new CalculatedTaxCollection()new TaxRuleCollection()),
            ]),
            $lineItems->getPrices()
        );
$type = $row['discr'];
                }
            } else {
                $type = $row['type'];
            }

            $price = new PriceStruct($type);

            $price->setId((int) $row['id']);

            if (isset($row['price'])) {
                $price->setPrice((float) $row['price']);
            }

            if (isset($row['subscription'])) {
                $price->setSubscription((bool) $row['subscription']);
            }

            if (isset($row['discount'])) {
                $price->setDiscount($row['discount']);
            }

            if (isset($row['duration'])) {
                
$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);
        $other->setPrice(new CalculatedPrice(100, 100, new CalculatedTaxCollection()new TaxRuleCollection()));
        $other->setStackable(true);

        $none = new LineItem('none', LineItem::PRODUCT_LINE_ITEM_TYPE, null, 1);
        $none->setPrice(new CalculatedPrice(100, 100, new CalculatedTaxCollection()new TaxRuleCollection()));
        $none->setStackable(false);

        $credit = new LineItem('credit', LineItem::CREDIT_LINE_ITEM_TYPE, null, 1);
        
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
                )
            );

            $toCalculate->add($lineItem);
        }
    }
}
self::item('a', 200),
                self::item('b', 100),
                self::item('c', 300),
            ],
            ['b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'c', 'c', 'c', 'c'],
        ];
    }

    private static function item(string $id, float $price): LineItem
    {
        $item = new LineItem($id, 'product');
        $item->setPrice(new CalculatedPrice($price$pricenew CalculatedTaxCollection()new TaxRuleCollection()));

        return $item;
    }
}
$lineItem = $cart->getLineItems()->first();

        static::assertNotNull($lineItem);
        static::assertInstanceOf(LineItem::class$lineItem);
        $collection = $lineItem->getExtensionOfType(OrderConverter::ORIGINAL_DOWNLOADS, OrderLineItemDownloadCollection::class);
        static::assertInstanceOf(OrderLineItemDownloadCollection::class$collection);
        static::assertEquals(1, $collection->count());

        $cart = $this->getCart();
        $cart->getLineItems()->clear();
        $lineItemA = (new LineItem('line-item-label-1', 'line-item-label-1', Uuid::randomHex()))
            ->setPrice(new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection()))
            ->setLabel('line-item-label-1')
            ->setStates([State::IS_DOWNLOAD]);
        $lineItemA->addExtension(OrderConverter::ORIGINAL_DOWNLOADS, $collection);
        $lineItemB = (new LineItem('line-item-label-2', 'line-item-label-2', Uuid::randomHex()))
            ->setPrice(new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection()))
            ->setLabel('line-item-label-2')
            ->setStates([State::IS_DOWNLOAD]);
        $cart->add($lineItemA);
        $cart->add($lineItemB);

        $order = $this->orderConverter->convertToOrder($cart$this->getSalesChannelContext(true)new OrderConversionContext());

        


    /** * Calculates the cheapest price considering the variant min purchase */
    private function calculateCheapestAvailablePrice(
        ListProduct $product,
        PriceRule $priceRule,
        ShopContextInterface $context
    ): Price {
        if ($priceRule->getUnit() instanceof Unit) {
            $priceRule->setPrice(
                $priceRule->getUnit()->getMinPurchase() * $priceRule->getPrice()
            );
            $priceRule->getUnit()->setPurchaseUnit(
                $priceRule->getUnit()->getMinPurchase() * $priceRule->getUnit()->getPurchaseUnit()
            );
            $priceRule->setPseudoPrice(
                $priceRule->getUnit()->getMinPurchase() * $priceRule->getPseudoPrice()
            );

            $priceRule->setRegulationPrice(
                $priceRule->getUnit()->getMinPurchase() * $priceRule->getRegulationPrice()
            );
$url = EnvironmentHelper::getVariable('REDIS_URL');

        if (!$url) {
            static::markTestSkipped('No redis server configured');
        }

        $this->getContainer()->get(Connection::class)->executeStatement('DELETE FROM cart');

        $redisCart = new Cart(Uuid::randomHex());
        $redisCart->add(
            (new LineItem('A', 'test'))
                ->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection()new TaxRuleCollection()))
        );

        $context = $this->getSalesChannelContext($redisCart->getToken());

        $factory = new RedisConnectionFactory('test-prefix-');
        $redis = $factory->create((string) $url);
        static::assertInstanceOf(\Redis::class$redis);
        $redis->flushAll();

        $persister = new RedisCartPersister($redis$this->getContainer()->get('event_dispatcher')$this->getContainer()->get(CartSerializationCleaner::class), false, 90);
        $persister->save($redisCart$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;
    }
}

/** * @internal */
$grossPrice = $netPrice + $taxValue;

        $calculatedTaxes = new CalculatedTaxCollection();
        $calculatedTaxes->add(new CalculatedTax($taxValue$taxRate$taxValue));

        $listPrice = null;
        if ($listPriceNet !== null) {
            $listPrice = ListPrice::createFromUnitPrice($netPrice$listPriceNet);
        }

        $product->setPrice(new CalculatedPrice(
            $grossPrice,
            $grossPrice,
            $calculatedTaxes,
            new TaxRuleCollection(),
            1,
            null,
            $listPrice
        ));

        return $product;
    }
}

#[Package('business-ops')] class LineItemUnitPriceRuleTest extends TestCase
{
    private LineItem $lineItem;

    protected function setUp(): void
    {
        parent::setUp();

        $this->lineItem = (new LineItem('A', 'product'))
            ->setPrice(
                new CalculatedPrice(100, 200, new CalculatedTaxCollection()new TaxRuleCollection())
            );
    }

    public function testRuleWithExactAmountMatch(): void
    {
        $rule = (new LineItemUnitPriceRule())->assign(['amount' => 100, 'operator' => Rule::OPERATOR_EQ]);

        $context = $this->createMock(SalesChannelContext::class);

        static::assertTrue(
            
$lineItem = new LineItem(Uuid::randomHex(), 'product');
        $lineItem->setDeliveryInformation(
            new DeliveryInformation(
                10,
                12.0,
                true,
                null,
                $this->deliveryTime
            )
        );
        $lineItem->setPrice(new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection()));
        $price = $lineItem->getPrice();
        static::assertNotNull($price);

        $delivery->expects(static::once())->method('getPositions')->willReturn(
            new DeliveryPositionCollection(
                [
                    new DeliveryPosition(
                        Uuid::randomHex(),
                        $lineItem,
                        1,
                        $price,
                        
Home | Imprint | This part of the site doesn't use cookies.