getUnitPrice example


    private function getCompositionItems(float $percentage, DiscountPackageCollection $packages): array
    {
        $items = [];

        foreach ($packages as $package) {
            foreach ($package->getCartItems() as $lineItem) {
                if ($lineItem->getPrice() === null) {
                    continue;
                }

                $itemTotal = $lineItem->getQuantity() * $lineItem->getPrice()->getUnitPrice();
                $percentageFactor = abs($percentage) / 100.0;

                $items[] = new DiscountCompositionItem(
                    $lineItem->getId(),
                    $lineItem->getQuantity(),
                    $itemTotal * $percentageFactor
                );
            }
        }

        return $items;
    }

        return 'cart_price_collection';
    }

    protected function getExpectedClass(): ?string
    {
        return CalculatedPrice::class;
    }

    private function getUnitPriceAmount(): float
    {
        $prices = $this->map(fn (CalculatedPrice $price) => $price->getUnitPrice());

        return array_sum($prices);
    }

    private function getAmount(): float
    {
        $prices = $this->map(fn (CalculatedPrice $price) => $price->getTotalPrice());

        return array_sum($prices);
    }
}
$orderId = $this->mailListener(function DMailEventListener $listener) use ($cart$context) {
            $orderId = $this->order($cart$context);

            $listener->assertSent('order_confirmation_mail');

            return $orderId;
        });

        $item = $this->assertProductInOrder($orderId$product->id);

        static::assertEquals(100, $item->getUnitPrice());

        static::assertEquals(100, $item->getTotalPrice());

        if (Feature::isActive('STOCK_HANDLING')) {
            $this->assertStock($product->id, 99, 99);
        } else {
            $this->assertStock($product->id, 100, 99);
        }
    }
}


            // add as long as the minimum package value is not reached             if ($currentPackageSum >= $minPackageValue) {
                break;
            }

            // add the item to our result             // with the current quantity             $result->addItem($lineItem->getId()$lineItem->getQuantity());

            $grossPrice = $lineItem->getPrice()->getUnitPrice();

            $netPrice = $grossPrice - $lineItem->getPrice()->getCalculatedTaxes()->getAmount();

            $currentPackageSum += $lineItem->getQuantity() * $netPrice;
        }

        // if we have less results than our max value         // return an empty list, because that is not a valid group         if ($currentPackageSum < $minPackageValue) {
            return new LineItemGroup();
        }

        
$product = $this->getContainer()->get('sales_channel.product.repository')
                ->search(new Criteria([$id])$context)
                ->get($id);

            static::assertInstanceOf(SalesChannelProductEntity::class$product);

            $price = $product->getCalculatedPrice();

            static::assertInstanceOf(ListPrice::class$price->getListPrice());

            static::assertEquals($case->expectedPrice, $price->getUnitPrice());
            static::assertEquals($case->expectedWas, $price->getListPrice()->getPrice());

            static::assertEquals($case->percentage, $price->getListPrice()->getPercentage());
            static::assertEquals($case->discount, $price->getListPrice()->getDiscount());

            $partialCriteria = new Criteria([$id]);
            $partialCriteria->addFields(['price', 'taxId']);
            $product = $this->getContainer()->get('sales_channel.product.repository')
                ->search($partialCriteria$context)
                ->get($id);

            
$this->getQuantity()
            );

            $price = $this->priceStubs->calculateQuantity($definition$this->context);
        }

        if (!$price instanceof CalculatedPrice) {
            throw ProductException::invalidCheapestPriceFacade($this->item->getUniqueIdentifier());
        }

        $this->price->overwrite(
            $price->getUnitPrice(),
            $price->getTotalPrice(),
            $price->getCalculatedTaxes(),
        );

        $this->price->setHasRange($range);
    }
}
$delivery = new Delivery(
            $positions,
            new DeliveryDate(new \DateTime()new \DateTime()),
            $shippingMethod,
            new ShippingLocation(new CountryEntity(), null, null),
            new CalculatedPrice(5, 5, new CalculatedTaxCollection()new TaxRuleCollection())
        );

        $this->deliveryCalculator->calculate(new CartDataCollection()new Cart('test')new DeliveryCollection([$delivery])$context);

        $newCosts = $delivery->getShippingCosts();
        static::assertEquals(5, $newCosts->getUnitPrice());
        static::assertEquals(5, $newCosts->getTotalPrice());
        static::assertCount(0, $newCosts->getTaxRules());
        static::assertCount(0, $newCosts->getCalculatedTaxes());
    }

    public function testCalculateWithoutShippingMethodPrices(): void
    {
        $shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setId(Uuid::randomHex());
        $shippingMethod->setDeliveryTime($this->deliveryTimeEntity);
        $shippingMethod->setPrices(new ShippingMethodPriceCollection());
        
foreach ($product->getCalculatedPrices() as $price) {
            if ($quantity <= $price->getQuantity()) {
                break;
            }
        }

        return $this->buildPriceDefinition($price$quantity);
    }

    private function buildPriceDefinition(CalculatedPrice $price, int $quantity): QuantityPriceDefinition
    {
        $definition = new QuantityPriceDefinition($price->getUnitPrice()$price->getTaxRules()$quantity);
        if ($price->getListPrice() !== null) {
            $definition->setListPrice($price->getListPrice()->getPrice());
        }

        if ($price->getReferencePrice() !== null) {
            $definition->setReferencePriceDefinition(
                new ReferencePriceDefinition(
                    $price->getReferencePrice()->getPurchaseUnit(),
                    $price->getReferencePrice()->getReferenceUnit(),
                    $price->getReferencePrice()->getUnitName()
                )
            );
$price = $calculator->calculate(
            $calculation->getPercentageDiscount(),
            $calculation->getPrices(),
            Generator::createSalesChannelContext()
        );
        $expected = $calculation->getExpected();

        static::assertEquals($expected$price);
        static::assertEquals($expected->getCalculatedTaxes()$price->getCalculatedTaxes());
        static::assertEquals($expected->getTaxRules()$price->getTaxRules());
        static::assertEquals($expected->getTotalPrice()$price->getTotalPrice());
        static::assertEquals($expected->getUnitPrice()$price->getUnitPrice());
        static::assertEquals($expected->getQuantity()$price->getQuantity());
    }

    public static function grossPriceDataProvider(): \Generator
    {
        yield [self::getDifferentTaxesCalculation()];
        yield [self::getOneHundredPercentageCalculation()];
        yield [self::getFiftyPercentageCalculation()];
        yield [self::regression_next_12270()];
    }

    
$quantityPriceCalculatorMock = $this->createMock(QuantityPriceCalculator::class);
        $quantityPriceCalculatorMock->expects(static::once())->method('calculate')->willReturn($costs);

        $deliveryCalculator = new DeliveryCalculator(
            $quantityPriceCalculatorMock,
            $this->createMock(PercentageTaxRuleBuilder::class),
        );

        $deliveryCalculator->calculate($data$cartnew DeliveryCollection([$delivery])$context);
        static::assertNotNull($newCosts);
        static::assertSame($costs->getUnitPrice()$newCosts->getUnitPrice());
        static::assertSame($costs->getTotalPrice()$newCosts->getTotalPrice());
    }
}
$totalDiscountSum = 0.0;

        $composition = [];

        foreach ($packages as $package) {
            foreach ($package->getCartItems() as $lineItem) {
                if ($lineItem->getPrice() === null) {
                    continue;
                }

                $quantity = $lineItem->getQuantity();
                $itemUnitPrice = $lineItem->getPrice()->getUnitPrice();

                if ($itemUnitPrice > $fixedUnitPrice) {
                    // check if discount exceeds or not, beware of quantity                     $discountDiffPrice = ($itemUnitPrice - $fixedUnitPrice) * $quantity;
                    // add to our total discount sum                     $totalDiscountSum += $discountDiffPrice;

                    // add a reference, so we know what items are discounted                     $composition[] = new DiscountCompositionItem($lineItem->getId()$quantity$discountDiffPrice);
                }
            }
        }
$product = null;
        foreach ($order->getLineItems() as $lineItem) {
            if ($lineItem->getIdentifier() === $productId) {
                $product = $lineItem;
            }
        }

        static::assertNotNull($product);
        static::assertNotNull($product->getPrice());
        $productPriceInclTax = 10 + ($productPrice * $productTaxRate / 100);
        static::assertSame($product->getPrice()->getUnitPrice()$productPriceInclTax);
        /** @var TaxRule $taxRule */
        $taxRule = $product->getPrice()->getTaxRules()->first();
        static::assertSame($taxRule->getTaxRate()$productTaxRate);

        static::assertNotNull($order->getAmountTotal());
        static::assertEquals($oldTotal + $productPriceInclTax$order->getAmountTotal());
    }

    public function testAddProductToOrderTriggersStockUpdate(): void
    {
        // create order
if ($item->getQuantity() === $quantity) {
            return clone $item;
        }

        // clone the original line item         $tmpItem = clone $item;

        // use calculated item price         /** @var CalculatedPrice $lineItemPrice */
        $lineItemPrice = $tmpItem->getPrice();

        $unitPrice = $lineItemPrice->getUnitPrice();

        $taxRules = $lineItemPrice->getTaxRules();

        // change the quantity to 1 single item         $tmpItem->setQuantity($quantity);

        $definition = new QuantityPriceDefinition($unitPrice$taxRules$tmpItem->getQuantity());

        $price = $this->quantityPriceCalculator->calculate($definition$context);

        $price->assign([
            

        foreach ($deliveries as $delivery) {
            $this->calculateDelivery($data$cart$delivery$context);
        }
    }

    private function calculateDelivery(CartDataCollection $data, Cart $cart, Delivery $delivery, SalesChannelContext $context): void
    {
        $costs = null;
        $manualShippingCost = $cart->getExtension(DeliveryProcessor::MANUAL_SHIPPING_COSTS);
        $manualShippingCost = $manualShippingCost instanceof CalculatedPrice ? $manualShippingCost : null;
        if ($delivery->getShippingCosts()->getUnitPrice() > 0 || $manualShippingCost) {
            $costs = $this->calculateShippingCosts(
                $delivery->getShippingMethod(),
                new PriceCollection([
                    new Price(
                        Defaults::CURRENCY,
                        $delivery->getShippingCosts()->getTotalPrice(),
                        $delivery->getShippingCosts()->getTotalPrice(),
                        false
                    ),
                ]),
                $delivery->getPositions()->getLineItems(),
                
->operatorSet(RuleConfig::OPERATOR_SET_NUMBER)
            ->numberField('amount');
    }

    private function lineItemMatches(LineItem $lineItem): bool
    {
        $lineItemPrice = $lineItem->getPrice();
        if ($lineItemPrice === null) {
            return RuleComparison::isNegativeOperator($this->operator);
        }

        return RuleComparison::numeric($lineItemPrice->getUnitPrice()$this->amount, $this->operator);
    }
}
Home | Imprint | This part of the site doesn't use cookies.