getCalculatedPrice example

->search($criteria$context);

                foreach ($assertions as $key => $assertion) {
                    $id = $ids->get($key);

                    $product = $products->get($id);

                    $error = sprintf('Case "%s": Product with key %s not found', $message$key);
                    static::assertInstanceOf(SalesChannelProductEntity::class$product$error);

                    $error = sprintf('Case "%s": Product with key %s, no calculated price found', $message$key);
                    static::assertInstanceOf(CalculatedPrice::class$product->getCalculatedPrice()$error);

                    $error = sprintf('Case "%s": Product with key %s, calculated price not match', $message$key);
                    static::assertEquals($assertion['price']$product->getCalculatedPrice()->getUnitPrice()$error);

                    $error = sprintf('Case "%s": Product with key %s, advanced prices count not match', $message$key);
                    static::assertEquals(\count($assertion['prices']), \count($product->getCalculatedPrices())$error);
                    foreach ($assertion['prices'] as $index => $expected) {
                        $price = $product->getCalculatedPrices()->get($index);

                        $error = sprintf('Case "%s": Product with key %s, advanced prices with index %s not match', $message$key$index);
                        static::assertInstanceOf(CalculatedPrice::class$price$error);
                        
'stock' => $product->getStock(),
        ];

        $lineItem->replacePayload($payload['purchasePrices' => true]);
    }

    private function getPriceDefinition(SalesChannelProductEntity $product, SalesChannelContext $context, int $quantity): QuantityPriceDefinition
    {
        $this->priceCalculator->calculate([$product]$context);

        if ($product->getCalculatedPrices()->count() === 0) {
            return $this->buildPriceDefinition($product->getCalculatedPrice()$quantity);
        }

        // keep loop reference to $price variable to get last quantity price in case of "null"         $price = $product->getCalculatedPrice();
        foreach ($product->getCalculatedPrices() as $price) {
            if ($quantity <= $price->getQuantity()) {
                break;
            }
        }

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


            $result = Shopware()->Container()->get(ProductSearchInterface::class)
                ->search($criteria$context);

            $products = array_values($result->getProducts());
            $products = array_map(function DListProduct $product) {
                $cheapestPrice = $product->getCheapestPrice();
                if (!$cheapestPrice instanceof Price) {
                    return $product;
                }
                $price = $cheapestPrice->getCalculatedPrice();
                $product = json_decode(json_encode($product, JSON_THROW_ON_ERROR), true);
                $product['cheapestPrice'] = $price;

                return $product;
            }$products);

            $success = true;
            $error = false;
            $data = $products;
            $total = $result->getTotalCount();
        } catch (Exception $e) {
            
'product' => $product,
        ]);
    }

    /** * @return array */
    public function convertProductPriceStruct(Price $price)
    {
        $data = $this->convertPriceStruct($price);
        $data['pseudopricePercent'] = null;
        $data['price'] = $this->formatPrice($price->getCalculatedPrice());
        $data['pseudoprice'] = $this->formatPrice($price->getCalculatedPseudoPrice());
        $data['referenceprice'] = $this->formatPrice($price->getCalculatedReferencePrice());
        $data['has_pseudoprice'] = $price->getCalculatedPseudoPrice() > $price->getCalculatedPrice();
        $data['price_numeric'] = $price->getCalculatedPrice();
        $data['pseudoprice_numeric'] = $price->getCalculatedPseudoPrice();
        $data['price_attributes'] = $price->getAttributes();
        $data['pricegroup'] = $price->getCustomerGroup()->getKey();
        $data['regulationPrice'] = $price->getCalculatedRegulationPrice();

        if ($price->getCalculatedPseudoPrice()) {
            $discount = 0;
            


            $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']);
            
$result = new ProductSearchResult(
            [$product->getNumber() => $product],
            1,
            [],
            $criteria,
            $this->contextService->getShopContext()
        );

        $service->updatePrices($criteria$result$this->contextService->getShopContext());

        if ($product->displayFromPrice()) {
            $data['priceStartingFrom'] = $product->getListingPrice()->getCalculatedPrice();
        }

        $data['price'] = $product->getListingPrice()->getCalculatedPrice();

        return $this->legacyEventManager->fireArticleByIdEvents($data$this);
    }

    /** * Creates different links for the product like `add to basket`, `add to note`, `view detail page`, ... * * @param int $categoryId * @param bool $addNumber * * @return array */


    /** * Calculates the product unit reference price for the passed * product price. */
    private function calculateReferencePrice(Price $price): float
    {
        if (!$price->getUnit() instanceof Unit) {
            throw new RuntimeException('Price must have a unit at this point. Is checked before calling this private method.');
        }
        $value = $price->getCalculatedPrice() / $price->getUnit()->getPurchaseUnit() * $price->getUnit()->getReferenceUnit();

        return round($value, 2);
    }
}
Home | Imprint | This part of the site doesn't use cookies.