getListPrice example

/** * @throws UnsupportedOperatorException */
    private function matchesListPriceCondition(LineItem $lineItem): bool
    {
        $calculatedPrice = $lineItem->getPrice();

        if (!$calculatedPrice instanceof CalculatedPrice) {
            return RuleComparison::isNegativeOperator($this->operator);
        }

        $listPrice = $calculatedPrice->getListPrice();

        $listPriceRatioAmount = null;
        if ($listPrice instanceof ListPrice) {
            $listPriceRatioAmount = $listPrice->getPercentage();
        }

        return RuleComparison::numeric($listPriceRatioAmount(float) $this->amount, $this->operator);
    }
}
ReferencePriceDto $reference,
        int $quantity = 1
    ): QuantityPriceDefinition {
        $price = $this->getPriceValue($prices$context);

        $taxId = $product->get('taxId');
        $definition = new QuantityPriceDefinition($price$context->buildTaxRules($taxId)$quantity);
        $definition->setReferencePriceDefinition(
            $this->buildReferencePriceDefinition($reference$units)
        );
        $definition->setListPrice(
            $this->getListPrice($prices$context)
        );
        $definition->setRegulationPrice(
            $this->getRegulationPrice($prices$context)
        );

        return $definition;
    }

    private function getPriceValue(PriceCollection $price, SalesChannelContext $context): float
    {
        /** @var Price $currency */
        
->read($definitionnew Criteria([$ids->get('entity')]), Context::createDefaultContext())
            ->get($ids->get('entity'));

        /** @var ArrayEntity $entity */
        static::assertInstanceOf(ArrayEntity::class$entity);

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

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

        static::assertInstanceOf(ListPrice::class$price->getListPrice());
        static::assertEquals(200, $price->getListPrice()->getPrice());
        static::assertEquals(-100, $price->getListPrice()->getDiscount());
        static::assertEquals(50, $price->getListPrice()->getPercentage());
    }
}
return;
        }

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

        static::assertInstanceOf(CalculatedPrice::class$price);

        static::assertEquals($expected->price, $price->getTotalPrice());

        static::assertEquals($expected->reference, $price->getReferencePrice()?->getPrice());

        static::assertEquals($expected->listPrice, $price->getListPrice()?->getPrice());
    }

    /** * @dataProvider taxStateWillBeUsedProvider */
    public function testTaxStateWillBeUsed(Entity $product, string $state, float $expected): void
    {
        $context = $this->createMock(SalesChannelContext::class);
        $context->method('getCurrencyId')->willReturn(Defaults::CURRENCY);
        $context->method('getContext')->willReturn(Context::createDefaultContext());
        $context->method('getTaxState')->willReturn($state);
        
$this->getContainer()->get('product.repository')
                ->create([$data], Context::createDefaultContext());

            $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)
                
$lineItem = $this->getContainer()
            ->get(ProductLineItemFactory::class)
            ->create(['id' => $ids->get('product'), 'referencedId' => $ids->get('product')]$context);

        $service->add($cart$lineItem$context);
        static::assertTrue($cart->has($ids->get('product')));

        // assert list price is calculated for default price         $lineItem = $cart->get($ids->get('product'));
        static::assertInstanceOf(LineItem::class$lineItem);
        static::assertInstanceOf(CalculatedPrice::class$lineItem->getPrice());
        static::assertInstanceOf(ListPrice::class$lineItem->getPrice()->getListPrice());
        $listPrice = $lineItem->getPrice()->getListPrice();
        static::assertSame(20.0, $listPrice->getPrice());

        $rules = [
            new LineItemListPriceRule(Rule::OPERATOR_GTE, 19),
            new LineItemListPriceRule(Rule::OPERATOR_GT, 19),
            new LineItemListPriceRule(Rule::OPERATOR_LTE, 21),
            new LineItemListPriceRule(Rule::OPERATOR_LT, 21),
            new LineItemListPriceRule(Rule::OPERATOR_EQ, 20),
            new LineItemListPriceRule(Rule::OPERATOR_NEQ, 15),
        ];

        
if (!$value instanceof PriceCollection) {
            throw CartException::absoluteSurchargeMissingPriceCollection($key);
        }
        if (!$value->has(Defaults::CURRENCY)) {
            throw CartException::missingDefaultPriceCollectionForSurcharge($key);
        }

        foreach ($value as $price) {
            $price->setGross(\abs($price->getGross()));
            $price->setNet(\abs($price->getNet()));

            if (!$price->getListPrice()) {
                continue;
            }
            $price->getListPrice()->setGross(\abs($price->getListPrice()->getGross()));
            $price->getListPrice()->setNet(\abs($price->getListPrice()->getNet()));
        }

        return new CurrencyPriceDefinition($value);
    }
}

        $json = '{"cb7d2554b0ce847cd82f3ac9bd1c0dfca":{"net":5.0,"gross":5.0,"currencyId":"b7d2554b0ce847cd82f3ac9bd1c0dfca","linked":true,"listPrice":{"net":"10","gross":"10","currencyId":"b7d2554b0ce847cd82f3ac9bd1c0dfca","linked":true},"regulationPrice":{"net":"10","gross":"10","currencyId":"b7d2554b0ce847cd82f3ac9bd1c0dfca","linked":true}}}';

        $field = new PriceField('test', 'test');

        $decoded = $this->serializer->decode($field$json);

        $price = $decoded->get(Defaults::CURRENCY);

        static::assertSame(5.0, $price->getNet());
        static::assertSame(5.0, $price->getGross());
        static::assertSame(10.0, $price->getListPrice()->getNet());
        static::assertSame(10.0, $price->getListPrice()->getGross());
        static::assertSame(10.0, $price->getRegulationPrice()->getNet());
        static::assertSame(10.0, $price->getRegulationPrice()->getGross());

        static::assertNull($price->getPercentage());
    }

    private function encode(array $data): string
    {
        $field = new PriceField('test', 'test');
        $existence = new EntityExistence('test', ['someId' => true], true, false, false, []);
        
if (!$value instanceof PriceCollection) {
            throw CartException::absoluteDiscountMissingPriceCollection($key);
        }
        if (!$value->has(Defaults::CURRENCY)) {
            throw CartException::missingDefaultPriceCollectionForDiscount($key);
        }

        foreach ($value as $price) {
            $price->setGross(\abs($price->getGross()) * -1);
            $price->setNet(\abs($price->getNet()) * -1);

            if (!$price->getListPrice()) {
                continue;
            }
            $price->getListPrice()->setGross(\abs($price->getListPrice()->getGross()) * -1);
            $price->getListPrice()->setNet(\abs($price->getListPrice()->getNet()) * -1);
        }

        return new CurrencyPriceDefinition($value);
    }
}
$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([
            'listPrice' => $lineItemPrice->getListPrice() ?? null,
        ]);

        $tmpItem->setPrice($price);

        return $tmpItem;
    }
}
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()
                )
            );
        }
$result = $this->productRepository->search(new Criteria(), Context::createDefaultContext());

        static::assertCount(2, $result);
        /** @var ProductCollection $products */
        $products = $result->getEntities();

        static::assertTrue($products->has('bf44b430d7cd47fcac93310edf4fe4e1'));
        $firstProduct = $products->get('bf44b430d7cd47fcac93310edf4fe4e1');
        static::assertInstanceOf(PriceCollection::class$firstProduct->getPrice());
        static::assertInstanceOf(Price::class$firstProduct->getPrice()->first());
        $firstListPrice = $firstProduct->getPrice()->first()->getListPrice();
        static::assertInstanceOf(Price::class$firstListPrice);
        static::assertSame(100.0, $firstListPrice->getNet());
        static::assertSame(5000.0, $firstListPrice->getGross());
        static::assertFalse($firstListPrice->getLinked());

        static::assertTrue($products->has('bf44b430d7cd47fcac93310edf4fe4e2'));
        $secondProduct = $products->get('bf44b430d7cd47fcac93310edf4fe4e2');
        static::assertInstanceOf(PriceCollection::class$secondProduct->getPrice());
        static::assertInstanceOf(Price::class$secondProduct->getPrice()->first());
        $secondListPrice = $secondProduct->getPrice()->first()->getListPrice();
        static::assertInstanceOf(Price::class$secondListPrice);
        
$calculatedTaxes,
            $taxRules,
            $definition->getQuantity(),
            $reference,
            $this->calculateListPrice($unitPrice$definition$config),
            $this->calculateRegulationPrice($definition$config)
        );
    }

    private function calculateListPrice(float $unitPrice, QuantityPriceDefinition $definition, CashRoundingConfig $config): ?ListPrice
    {
        $listPrice = $definition->getListPrice();
        if (!$listPrice) {
            return null;
        }

        if (!$definition->isCalculated()) {
            $listPrice = $this->round($listPrice$config);
        }

        return ListPrice::createFromUnitPrice($unitPrice$listPrice);
    }

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

        $ids = new TestDataCollection();

        $data = [
            [
                
/** * @throws UnsupportedOperatorException */
    private function matchesListPriceCondition(LineItem $lineItem): bool
    {
        $calculatedPrice = $lineItem->getPrice();

        if (!$calculatedPrice instanceof CalculatedPrice) {
            return RuleComparison::isNegativeOperator($this->operator);
        }

        $listPrice = $calculatedPrice->getListPrice();

        $listPriceAmount = null;
        if ($listPrice instanceof ListPrice) {
            $listPriceAmount = $listPrice->getPrice();
        }

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