Hello Magento Folks?,
I hope all are awesome. Welcome to the Magento How-To Magento 2 blog. Today I am here with another interesting solution of the Magento 2: Show Lowest and Highest Price on Category Page For Configurable Product. Don’t forget to visit our last published blog about How to Custom Maintenance mode Page in Magento 2.
Let’s get started?
Contents
Introduction:
When we discuss Magento there are many advantages but when we focus on disadvantages then there is the incapability of displaying a price range for configurable products. Many times customers might be distracted with this and eliminate it to prefer shopping. So, to overcome this problem I will illustrate to you in the solution below.
Steps to Show Lowest and Highest Price on Category Page For Configurable Product:
Step 1: First, we need to create a “Registration.php” file inside our extension at the following path..
app\code\Vendor\Extension
1 2 3 4 5 6 7 8 9 10 11 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Extension', __DIR__ ); |
Step 2: After that, we need to create a “module.xml” file inside the extension, etc folder.
app\code\Vendor\Extension\etc
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Extension" setup_version="1.0.0" schema_version="1.0.0"/> </config> |
Step 3: After that, we need to create “di.xml” file inside below path folder to add file in module.
app\code\Vendor\Extension\etc\frontend
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Block\Product\ListProduct"> <plugin name="price_range" type="Vendor\Extension\Plugin\Block\Product\ListProduct" sortOrder="1"/> </type> </config> |
Step 4: After that, we need to create a “ListProduct.php” file inside the path folder to add the file in the module.
app\code\Vendor\Extension\Plugin\Block\Product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
<?php namespace Vendor\Extension\Plugin\Block\Product; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; class ListProduct { protected $listProductBlock; protected $configurableProduct; protected $pricingHelper; protected $logger; public function __construct( Configurable $configurableProduct, \Magento\Framework\Pricing\Helper\Data $pricingHelper, \Psr\Log\LoggerInterface $logger ) { $this->configurableProduct = $configurableProduct; $this->pricingHelper = $pricingHelper; $this->logger = $logger; } public function aroundGetProductPrice( \Magento\Catalog\Block\Product\ListProduct $subject, \Closure $proceed, \Magento\Catalog\Model\Product $product) { if (Configurable::TYPE_CODE !== $product->getTypeId()) { return $proceed($product); } $this->listProductBlock = $subject; $priceText = $this->getPriceRange($product); return $priceText; } public function getPriceRange($product) { $childProductPrice = []; $childProducts = $this->configurableProduct->getUsedProducts($product); foreach($childProducts as $child) { $price = number_format($child->getPrice(), 2, '.', ''); $finalPrice = number_format($child->getFinalPrice(), 2, '.', ''); if($price == $finalPrice) { $childProductPrice[] = $price; } else if($finalPrice < $price) { $childProductPrice[] = $finalPrice; } } $max = $this->pricingHelper->currencyByStore(max($childProductPrice)); $min = $this->pricingHelper->currencyByStore(min($childProductPrice)); if($min==$max){ return $this->getPriceRender($product, "$min", ''); } else { return $this->getPriceRender($product, "$min-$max", ''); } } protected function getPriceRender($product, $price, $text='') { return $this->listProductBlock->getLayout()->createBlock('Magento\Framework\View\Element\Template') ->setTemplate('Vendor_Extension::product/price/range/price.phtml') ->setData('price_id', 'product-price-'.$product->getId()) ->setData('display_label', $text) ->setData('product_id', $product->getId()) ->setData('display_value', $price)->toHtml(); } } |
Step 5: Lastly, Create the “price.phtml” file inside the path folder in the module.
app\code\Vendor\Extension\view\frontend\templates\Product\Price\range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<div class="price-box range second price-final_price" data-role="priceBox" data-product-id="<?php /* @escapeNotVerified */ echo $block->getProductId() ?>"> <span class="special-price"> <span class="price-container price-final_price"> <?php if ($block->getDisplayLabel()): ?> <span class="price-label"><?php /* @escapeNotVerified */ echo $block->getDisplayLabel(); ?></span> <?php endif; ?> <span <?php if ($block->getPriceId()): ?> id="<?php /* @escapeNotVerified */ echo $block->getPriceId() ?>"<?php endif;?> data-price-amount="" data-price-type="finalPrice1" class="price-wrapper"> <span class="price"><?php /* @escapeNotVerified */ echo $block->getDisplayValue();?></span> </span> </span> </span> </div> |
That’s It.
Over to You
So, that was all for the day! With the help of these codes, you can successfully achieve the task of showing the lowest and highest price on the category page for the configurable products in Magento 2. You are free to play around and customize these codes according to your needs for fetching data. If you face any issues while implementing, then contact our Support Team. We will be happy to help you. If the article was helpful then do share with your Magento friends.
Happy Coding?