How To

Magento 2: Show Out of Stock Product Price in Product Detail Page and Category Page

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”.

Quick Intro:

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. 

Steps to 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.

Conclusion:

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.

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

1 day ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

4 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

5 days ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

6 days ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

6 days ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

1 week ago