0

In my Magento 2 instance I have a few configurable products however on the frontend the word "From" is missing before the price on both the listing and detail pages of the catalog.

The actual configurable products function as expected eg the price changes when the user selects different options.

Could someone advise if there is a setting which needs to be enabled for the wording "From" to be shown on 1st page load

1

1 Answer 1

1

I did this by extending Magento\ConfigurableProduct\Pricing\Price\FinalPrice with functions for getting min/max final price of configurable products and inserting the label only when needed (when there are simple products with different prices inside the configurable).

Gonna post my solution for others that might need help with this:

{Vendor}/{Module}/etc/di.xml

<?php
namespace Vendor\Module\Pricing\Price;

use Magento\Framework\App\ObjectManager;

class FinalPrice extends \Magento\ConfigurableProduct\Pricing\Price\FinalPrice
{

    protected $priceResolver;

    protected $maxFinalAmount;

    protected $minFinalAmount;

    protected $lowestPriceOptionsProvider;

    protected $configurableType;

    public function __construct(
        \Magento\Framework\Pricing\SaleableInterface $saleableItem,
        $quantity,
        \Magento\Framework\Pricing\Adjustment\CalculatorInterface $calculator,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        \Magento\ConfigurableProduct\Pricing\Price\PriceResolverInterface $priceResolver,
        \Magento\ConfigurableProduct\Pricing\Price\LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider = null,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurableType
    ) {
        parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency, $priceResolver);
        $this->priceResolver = $priceResolver;
        $this->lowestPriceOptionsProvider = $lowestPriceOptionsProvider ?:
        ObjectManager::getInstance()->get(\Magento\ConfigurableProduct\Pricing\Price\LowestPriceOptionsProviderInterface::class);
        $this->configurableType = $configurableType ?:
        ObjectManager::getInstance()->get(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class);
    }

    public function getMaxFinalAmount()
    {
        if (null === $this->maxFinalAmount) {
            $this->maxFinalAmount = $this->doGetMaxFinalAmount() ?: false;
        }
        return $this->maxFinalAmount;
    }

    protected function doGetMaxFinalAmount()
    {
        $maxAmount = null;
        $usedProducts = $this->configurableType->getUsedProducts($this->product);

        foreach ($usedProducts as $product) {
            $childPriceAmount = $product->getPriceInfo()->getPrice('final_price')->getAmount();
            if (!$maxAmount || ($childPriceAmount->getValue() > $maxAmount->getValue())) {
                $maxAmount = $childPriceAmount;
            }
        }
        return $maxAmount;
    }

    public function getMinFinalAmount()
    {
        if (null === $this->minFinalAmount) {
            $this->minFinalAmount = $this->doGetMinFinalAmount() ?: parent::getAmount();
        }
        return $this->minFinalAmount;
    }

    protected function doGetMinFinalAmount()
    {
        $minAmount = null;
        $usedProducts = $this->lowestPriceOptionsProvider->getProducts($this->product);

        foreach ($usedProducts as $product) {
            $childPriceAmount = $product->getPriceInfo()->getPrice('final_price')->getAmount();
            if (!$minAmount || ($childPriceAmount->getValue() < $minAmount->getValue())) {
                $minAmount = $childPriceAmount;
            }
        }
        return $minAmount;
    }
}

Then you can call the new function getMinFinalAmount and getMaxFinalAmount from your custom:

.../Magento_ConfigurableProduct/templates/product/price/final_price.phtml

$minAmount = $finalPriceModel->getMinFinalAmount(); $maxAmount = $finalPriceModel->getMaxFinalAmount();

And display the label where you want like this:

<?php if ($minAmount < $maxAmount) { ?>
    <span class="price-label"><?php echo __('Price from:') ?></span>
<?php } ?>

All that's left is to use JS to hide the Price from label when a simple configuration is selected.


Answer is copied from Web .

2
  • Thanks for the answer, isnt this functionality native in Magento? It is in Magento 1
    – Goose84
    Commented Feb 12, 2018 at 14:52
  • Hi , This is in magento 2 but for that you should have to set multiple prices with configurable Product from admin so it will automatically use From $Price with product . if my answer helped me please accept this answer as solution . Commented Feb 14, 2018 at 6:35

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.