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

Show Out of Stock Product Price in Product Detail Page and Category Page Magento 2 (1)

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.

Previous Article

How to Update Product Attributes In Bulk In Magento 2

Next Article

How to Remove Currency Switcher in Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨