invalidPriceDefinition example


    public function change(array $changes): void
    {
        $mapped = [];
        foreach ($changes as $change) {
            $mapped[(string) $change['to']] = $change['price'];
        }

        // check for "null" value         if (!\array_key_exists('', $mapped)) {
            throw ProductException::invalidPriceDefinition();
        }

        $last = $mapped[null];
        unset($mapped[null]);

        \ksort($mapped, \SORT_NUMERIC);
        $max = \max(\array_keys($mapped));

        $mapped[$max + 1] = $last;

        $this->prices->clear();

        
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();
        }

        $taxRules = $this->percentageTaxRuleBuilder->buildRules($prices->sum());

        $definition = new QuantityPriceDefinition($value$taxRules$quantity);

        
/** * @param array<string, array{gross:float, net:float, linked?:bool}> $price * * @return array<string, array{gross:float, net:float, linked?:bool}> */
    private function validatePrice(array $price): array
    {
        $price = $this->resolveIsoCodes($price);

        if (!\array_key_exists(Defaults::CURRENCY, $price)) {
            throw CartException::invalidPriceDefinition();
        }

        foreach ($price as $id => $value) {
            if (!Uuid::isValid($id)) {
                throw CartException::invalidPriceDefinition();
            }

            if (!\array_key_exists('gross', $value)) {
                throw CartException::invalidPriceDefinition();
            }

            
private function calculate(?PriceDefinitionInterface $definition, LineItemCollection $goods, SalesChannelContext $context): CalculatedPrice
    {
        if ($definition instanceof PercentagePriceDefinition) {
            return $this->percentageCalculator->calculate($definition->getPercentage()$goods->getPrices()$context);
        }

        if ($definition instanceof CurrencyPriceDefinition) {
            return $this->currencyCalculator->calculate($definition->getPrice()$goods->getPrices()$context);
        }

        throw CartException::invalidPriceDefinition();
    }
}
'referencedId' => $productId,
            'type' => 'nonexistenttype',
            'stackable' => 1,
            'removable' => 1,
            'quantity' => 1,
        ];

        $request = new Request([]['lineItems' => [$productId => $lineItemData]]);
        $cart = new Cart(Uuid::randomHex());
        $context = $this->createMock(SalesChannelContext::class);

        $exception = CartException::invalidPriceDefinition();
        $this->lineItemRegistryMock->expects(static::once())
            ->method('create')
            ->with($lineItemData$this->createMock(SalesChannelContext::class))
            ->willThrowException($exception);

        $this->cartService->expects(static::never())->method('add');

        $this->expectExceptionObject($exception);
        $this->controller->addLineItems($cartnew RequestDataBag($request->request->all())$request$context);
    }

    
$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();
    }

    private function overwrite(QuantityPriceDefinition $definition): void
    {
        
Home | Imprint | This part of the site doesn't use cookies.