getCurrencyPrice example


    public function count(): int
    {
        return $this->prices->count();
    }

    private function getPriceForTaxState(PriceCollection $price, SalesChannelContext $context): float
    {
        $currency = $price->getCurrencyPrice($this->context->getCurrencyId());

        if (!$currency instanceof Price) {
            throw ProductException::invalidPriceDefinition();
        }

        if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
            return $currency->getGross();
        }

        return $currency->getNet();
    }
}
$entity = $this->getContainer()->get(EntityReaderInterface::class)
            ->read($definitionnew Criteria([$ids->get('with-was')]), Context::createDefaultContext())
            ->get($ids->get('with-was'));

        static::assertNotNull($entity);

        $price = $entity->get('data');

        /** @var PriceCollection $price */
        static::assertInstanceOf(PriceCollection::class$price);

        $price = $price->getCurrencyPrice(Defaults::CURRENCY);
        /** @var Price $price */
        static::assertInstanceOf(Price::class$price);

        static::assertInstanceOf(Price::class$price->getListPrice());
        static::assertEquals(2, $price->getListPrice()->getNet());
    }

    public function testListPriceInCriteriaParts(): void
    {
        $context = WriteContext::createFromContext(Context::createDefaultContext());

        
$versionContext = $context->createWithVersionId($versionId);

        $this->productRepository->update([
            [
                'id' => $id,
                'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 1000, 'net' => 1000, 'linked' => false]],
            ],
        ]$versionContext);

        $product = $this->productRepository->search(new Criteria([$id])$context)->first();
        static::assertInstanceOf(ProductEntity::class$product);
        $price = $product->getCurrencyPrice(Defaults::CURRENCY);
        static::assertInstanceOf(Price::class$price);
        static::assertEquals(100, $price->getGross());
        static::assertEquals(10, $price->getNet());

        $product = $this->productRepository->search(new Criteria([$id])$versionContext)->first();
        static::assertInstanceOf(ProductEntity::class$product);
        $price = $product->getCurrencyPrice(Defaults::CURRENCY);
        static::assertInstanceOf(Price::class$price);
        static::assertEquals(1000, $price->getGross());
        static::assertEquals(1000, $price->getNet());

        

        $definition->setRegulationPrice(
            $this->getRegulationPrice($prices$context)
        );

        return $definition;
    }

    private function getPriceValue(PriceCollection $price, SalesChannelContext $context): float
    {
        /** @var Price $currency */
        $currency = $price->getCurrencyPrice($context->getCurrencyId());

        $value = $this->getPriceForTaxState($currency$context);

        if ($currency->getCurrencyId() !== $context->getCurrency()->getId()) {
            $value *= $context->getContext()->getCurrencyFactor();
        }

        return $value;
    }

    private function getPriceForTaxState(Price $price, SalesChannelContext $context): float
    {
$definition = new QuantityPriceDefinition(
            $this->price->getUnitPrice() + $discount->getUnitPrice(),
            $this->price->getTaxRules(),
            $this->getQuantity()
        );

        $this->overwrite($definition);
    }

    protected function getPriceForTaxState(PriceCollection $price, SalesChannelContext $context): float
    {
        $currency = $price->getCurrencyPrice($this->context->getCurrencyId());

        if (!$currency instanceof Price) {
            throw CartException::invalidPriceDefinition();
        }

        if ($context->getTaxState() === CartPrice::TAX_STATE_GROSS) {
            return $currency->getGross();
        }

        return $currency->getNet();
    }

    
/** * @internal */
    public function __construct(
        private readonly QuantityPriceCalculator $priceCalculator,
        private readonly PercentageTaxRuleBuilder $percentageTaxRuleBuilder
    ) {
    }

    public function calculate(PriceCollection $price, CalculatedPriceCollection $prices, SalesChannelContext $context, int $quantity = 1): CalculatedPrice
    {
        $currency = $price->getCurrencyPrice($context->getCurrencyId());

        if (!$currency) {
            throw CartException::invalidPriceDefinition();
        }

        $value = $context->getTaxState() === CartPrice::TAX_STATE_GROSS ? $currency->getGross() : $currency->getNet();

        if ($currency->getCurrencyId() !== $context->getCurrencyId()) {
            $value *= $context->getCurrency()->getFactor();
        }

        
return $prices[$ruleId];
        }

        return null;
    }

    /** * @param array<mixed> $price */
    private function getPriceValue(array $price, Context $context): ?float
    {
        $currency = $this->getCurrencyPrice($price['price']$context->getCurrencyId());

        if (!$currency) {
            return null;
        }

        $value = $context->getTaxState() === CartPrice::TAX_STATE_GROSS ? $currency['gross'] : $currency['net'];

        if ($currency['currencyId'] !== $context->getCurrencyId()) {
            $value *= $context->getCurrencyFactor();
        }

        
->getResult();

        static::assertEquals(1, $result->count());

        $element = $result->first();
        static::assertNotNull($element);
        static::assertEquals(3, $element->getTotal());
        static::assertEquals('Test Cross Selling', $element->getCrossSelling()->getName());

        $lastPrice = 0;
        foreach ($element->getProducts() as $product) {
            $productPrice = $product->getCurrencyPrice(Defaults::CURRENCY);
            static::assertNotNull($productPrice);
            static::assertGreaterThanOrEqual($lastPrice$productPrice->getGross());
            $lastPrice = $productPrice->getGross();
        }
    }

    public function testLoadForProduct(): void
    {
        $productId = Uuid::randomHex();

        $productData = $this->getProductData($productId);
        
$this->repository->create([$data]$this->context);

        /** @var ProductEntity $product */
        $product = $this->repository
            ->search(new Criteria([$id])$this->context)
            ->get($id);

        static::assertInstanceOf(PriceCollection::class$product->getPrice());

        static::assertEquals(
            new Price(Defaults::CURRENCY, 10, 15, false),
            $product->getPrice()->getCurrencyPrice(Defaults::CURRENCY)
        );
    }

    public function testWriteMultipleCurrencyPrices(): void
    {
        $id = Uuid::randomHex();

        $this->getContainer()->get('currency.repository')->create(
            [
                [
                    'id' => $id,
                    
public function setPrice(PriceCollection $price): void
    {
        $this->price = $price;
    }

    public function getCurrencyPrice(string $currencyId): ?Price
    {
        if ($this->price === null) {
            return null;
        }

        return $this->price->getCurrencyPrice($currencyId);
    }

    public function getManufacturerNumber(): ?string
    {
        return $this->manufacturerNumber;
    }

    public function setManufacturerNumber(?string $manufacturerNumber): void
    {
        $this->manufacturerNumber = $manufacturerNumber;
    }

    
// no break             default:
                $rules = $this->percentageTaxRuleBuilder->buildRules(
                    $calculatedLineItems->getPrices()->sum()
                );
        }

        if ($manualShippingCost !== null) {
            $price = $manualShippingCost->getTotalPrice();
        } else {
            $price = $this->getCurrencyPrice($priceCollection$context);
        }

        $definition = new QuantityPriceDefinition($price$rules, 1);

        return $this->priceCalculator->calculate($definition$context);
    }

    private function getCurrencyPrice(PriceCollection $priceCollection, SalesChannelContext $context): float
    {
        /** @var Price $price */
        $price = $priceCollection->getCurrencyPrice($context->getCurrency()->getId());

        

    protected $unitId;

    /** * @var PriceCollection */
    protected $price;

    public function getCurrencyPrice(string $currencyId): ?Price
    {
        return $this->price->getCurrencyPrice($currencyId);
    }

    public function getVariantId(): string
    {
        return $this->variantId;
    }

    public function setVariantId(string $variantId): void
    {
        $this->variantId = $variantId;
    }

    


        $quantityInformation->setPurchaseSteps(
            $product->getPurchaseSteps() ?? 1
        );

        $lineItem->setQuantityInformation($quantityInformation);

        $purchasePrices = null;
        $purchasePricesCollection = $product->getPurchasePrices();
        if ($purchasePricesCollection !== null) {
            $purchasePrices = $purchasePricesCollection->getCurrencyPrice(Defaults::CURRENCY);
        }

        $payload = [
            'isCloseout' => $product->getIsCloseout(),
            'customFields' => $product->getCustomFields(),
            'createdAt' => $product->getCreatedAt() ? $product->getCreatedAt()->format(Defaults::STORAGE_DATE_TIME_FORMAT) : null,
            'releaseDate' => $product->getReleaseDate() ? $product->getReleaseDate()->format(Defaults::STORAGE_DATE_TIME_FORMAT) : null,
            'isNew' => $product->isNew(),
            'markAsTopseller' => $product->getMarkAsTopseller(),
            'purchasePrices' => $purchasePrices ? json_encode($purchasePrices, \JSON_THROW_ON_ERROR) : null,
            'productNumber' => $product->getProductNumber(),
            
$this->productRepository->create($products, Context::createDefaultContext());

        $criteria = new Criteria([$parentId$greenId$redId]);
        $context = Context::createDefaultContext();
        $context->setConsiderInheritance(false);
        $products = $this->productRepository->search($criteria$context);

        /** @var ProductEntity $parent */
        $parent = $products->get($parentId);
        static::assertInstanceOf(ProductEntity::class$parent);
        static::assertInstanceOf(TaxEntity::class$parent->getTax());
        static::assertInstanceOf(Price::class$parent->getCurrencyPrice(Defaults::CURRENCY));
        static::assertEquals(50, $parent->getCurrencyPrice(Defaults::CURRENCY)->getGross());

        /** @var ProductEntity $red */
        $red = $products->get($redId);

        // check red product contains full inheritance of parent         static::assertInstanceOf(ProductEntity::class$red);

        // has no own tax         static::assertNull($red->getTax());
        static::assertNull($red->getTaxId());
        

        $connection = $this->createMock(Connection::class);
        $connection->method('fetchAllKeyValue')->willReturn([
            'USD' => self::USD_ID,
        ]);

        $stubs = new ScriptPriceStubs($connection$this->createMock(QuantityPriceCalculator::class)$this->createMock(PercentagePriceCalculator::class));

        $actual = $stubs->build($prices);

        foreach ($expected as $expectedPrice) {
            $actualPrice = $actual->getCurrencyPrice($expectedPrice->getCurrencyId());

            static::assertInstanceOf(Price::class$actualPrice);
            static::assertEquals($expectedPrice->getNet()$actualPrice->getNet());
            static::assertEquals($expectedPrice->getGross()$actualPrice->getGross());
            static::assertEquals($expectedPrice->getLinked()$actualPrice->getLinked());
        }
    }

    public static function priceCases(): \Generator
    {
        yield 'manual price definition' => [
            [
Home | Imprint | This part of the site doesn't use cookies.