2

For example, I have a configurable product like below:

  • configurable product, price 999
  • its simple product: size S, price 799, special price 599
  • its simple product: size M, price 799
  • its simple product: size L, price 999
  • its simple product: size XL, price 799
  • its simple product: size XXL, price 799

I want the initial price to be taken from the configurable product on which I set the price instead of the most cheapest simple product or text like "From $599".

How to do it programmatically?

1 Answer 1

0

Something like this in your block class could work? You could then call this method in your template via $lowestPrice = $block->getLowestVariantPrice($configurableProduct);

private \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency;
private \Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterface $configurableOptionsProvider;

/**
 * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
 * @param \Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterface $configurableOptionsProvider
 */
public function __construct(
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    \Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterface $configurableOptionsProvider
) {
    $this->priceCurrency = $priceCurrency;
    $this->configurableOptionsProvider = $configurableOptionsProvider;
}

/**
 * @param ProductInterface $product
 * @return ?string
 */
public function getLowestVariantPrice(\Magento\Catalog\Api\Data\ProductInterface $product): ?string
{
    $lowestPrice = null;
    /** @var \Magento\Catalog\Api\Data\ProductInterface $variant */
    foreach ($this->configurableOptionsProvider->getProducts($product) as $variant) {
        $variantAmount = (float)$variant->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
        if ($lowestPrice === null || ($variantAmount < $lowestPrice)) {
            $lowestPrice = $variantAmount;
        }
    }
    return $lowestPrice !== null ? $this->formatPrice($lowestPrice) : null;
}

/**
 * @param string|float $price
 * @return string
 */
private function formatPrice($price): string
{
    return $this->priceCurrency->format($price, false, 2);
}
1
  • Thanks for your answear but that's not the point. By default, magento displays the price on the configurable product: "From: $599", and when you select a variant - the price reloads. I want it to display the price taken from the configurable product (that's how it doesn't work now) and change according to the selected size (that's how the standard now works).
    – GieDe
    Feb 13, 2023 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.