Hello Magento Folks,
We are back with a new article of our “How To” blog series for Magento 2. At the moment we will learn Magento 2: Show Out of Stock Product Price in Product Detail Page and Category Page. In the last blog article, I have Solved Magento 2: “There has been an error processing your request”.
Contents
Basically, when any product is out of stock then the price is not displayed on the product detail page and category page. If you desire to display it for the awareness of your customers then you have to apply the below steps. Let’s get started with Magento 2:Show Out of Stock Product Price in Product Detail Page and Category Page.Â
Step 1: Firstly, we will need to create a “di.xml” file inside our extension at the below path.
app\code\Vendor\Extension\etc\di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Vendor\Extension\Pricing\Render\FinalPriceBox" /> </config>
Step 2: After that, we will need to create a “FinalPriceBox.php” file inside the below folder path of the extension.
app\code\Vendor\Extension\Pricing\Render\FinalPriceBox.php
<?php namespace Vendor\Extension\Pricing\Render; use Magento\Msrp\Pricing\Price\MsrpPrice; use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox; class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox { protected function _toHtml() { $result = parent::_toHtml(); if(!$result) { $result = BasePriceBox::_toHtml(); try { $msrpPriceType = $this->getSaleableItem()->getPriceInfo()->getPrice('msrp_price'); } catch (\InvalidArgumentException $e) { $this->_logger->critical($e); return $this->wrapResult($result); } $product = $this->getSaleableItem(); if ($msrpPriceType->canApplyMsrp($product) && $msrpPriceType->isMinimalPriceLessMsrp($product)) { $msrpBlock = $this->rendererPool->createPriceRender( MsrpPrice::PRICE_CODE, $this->getSaleableItem(), [ 'real_price_html' => $result, 'zone' => $this->getZone(), ] ); $result = $msrpBlock->toHtml(); } return $this->wrapResult($result); } return $result; } }
That’s It.
Expectantly, you all will have successfully implemented the above solution to show out of stock product price on the product detail page and category page. If you feel any type of difficulties in the implementation of the above solution then let me know in the comments section below.Â
Share the solution with your friends.
Happy Reading.
The mobile app development field has witnessed a rapid revolution over the past few years.…
Hello Magento mates, Today we will learn to add a call JS on the checkout…
Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…
Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…
In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…
Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…
View Comments
its not working magento 2.3.1 cloud version , I have install successfully
please let me know its perfect solution ?
What's the error you are facing?
Yeah same here. Not working in 2.3.5
can you specify which error you are facing in this one
This should not working for configurable product.
Hi, not work in magento 2.3.5
Kindly check your extension is enabled and your code is getting called.