In this blog, we’ll talk about “Get Price Range of Configurable Product in Magento 2” and understand the methods to retrieve minimum and maximum prices.
To display the Price Range of a configurable product in Magento, you can use a method provided by Magento to get the Minimum and Maximum Price. This post will explain how to obtain the range in a template file.
In your module structure create a layout file catalog_product_view_type_configurable.xml, in my case the file path is app/code/Webkul/PriceRange/view/base/layout/catalog_product_view_type_configurable.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.price">
<block class="Magento\ConfigurableProduct\Block\Product\View\Type\Configurable" name="wk.info.pricerange" template="Webkul_PriceRange::product/price_range.phtml" />
</referenceBlock>
</body>
</page>
Now, we will create template file price_range.phtml at path app/code/Webkul/PriceRangeCustomisation/view/base/templates/
<?php
$currentProduct = $this->getProduct();
$regularPrice = $currentProduct->getPriceInfo()->getPrice('regular_price');
?>
<div class='price-box'>
<span class="price">
<?php
echo $regularPrice->getMinRegularAmount().'-'.$regularPrice->getMaxRegularAmount();
?>
</span>
</div>
Also, the methods getMinRegularAmount() and getMaxRegularAmount() returns the price of In Stock associated products only.
For further studying the method definition, you can refer to the file magento_root_directory/vendor/magento/module-configurable-product/Pricing/Price/ConfigurableRegularPrice.php
That’s all for the post, feedback is welcomed.
Be the first to comment.