magento 2 show price for out of stock products

Full Tutorial On Magento 2 Show Price For Out Of Stock Products

by admin

In this article, we will show you a comprehensive tutorial on Magento 2 show price for out of stock products.

With the default Magento, when a product is out of stock, its price is not displayed on the product page and category page.

However, some store owners want to display the price so customers can be aware of the products.

And today, we will show you step by step how to display out of stock product price on the product page and category page.

Let’s get started!

Full Tutorial On Magento 2 Show Price For Out Of Stock Products

We have 2 methods for you for Magento 2 Show Price For Out Of Stock Products.

Method 1: For simple products

First, create a new module named Vendor/Module/etc/frontend/di.xml and add the following code.

<?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\Module\Pricing\Render\FinalPriceBox" />
</config>

Then, create a new php file named Vendor/Module/Pricing/Render/FinalPriceBox.php and add the following code.

namespace Vendor\Module\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 {
                /** @var MsrpPrice $msrpPriceType */
                $msrpPriceType = $this->getSaleableItem()->getPriceInfo()->getPrice('msrp_price');
            } catch (\InvalidArgumentException $e) {
                $this->_logger->critical($e);
                return $this->wrapResult($result);
            }

            //Renders MSRP in case it is enabled
            $product = $this->getSaleableItem();
            if ($msrpPriceType->canApplyMsrp($product) && $msrpPriceType->isMinimalPriceLessMsrp($product)) {
                /** @var BasePriceBox $msrpBlock */
                $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;
    }
}

And that’s it. Go to your front-end store to check the change.

Method 2: For configurable products

Usually, the first method also works for configurable products. But if it doesn’t, then you can use this method.

For any out-of-stock product, in your di.xml, add the following code:

<preference for="Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox" type="<Vendor>\<Module>\Pricing\Render\FinalPriceBox" />

And in \\Pricing\Render\FinalPriceBox.php, add the following code:

namespace <Vendor>\<Module>\Pricing\Render;

use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox;

class FinalPriceBox extends \Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox
{
    protected function _toHtml()
    {
        $result = BasePriceBox::_toHtml();
        //Renders MSRP in case it is enabled
        if ($this->isMsrpPriceApplicable()) {
            /** @var BasePriceBox $msrpBlock */
            $msrpBlock = $this->rendererPool->createPriceRender(
                MsrpPrice::PRICE_CODE,
                $this->getSaleableItem(),
                [
                    'real_price_html' => $result,
                    'zone' => $this->getZone(),
                ]
            );
            $result = $msrpBlock->toHtml();
        }

        return $this->wrapResult($result);
    }
}

For all out-of-stock sub-products, in your di.xml, add the following code:

<type name="Magento\ConfigurableProduct\Model\ResourceModel\Product\LinkedProductSelectBuilder">
    <arguments>
        <argument name="baseSelectProcessor" xsi:type="object"><Vendor>\<Module>\Model\ConfigurableProduct\ResourceModel\Product\StockStatusBaseSelectProcessor</argument>
    </arguments>
</type>

And remove stock select.

namespace <Vendor>\<Module>\Model\ConfigurableProduct\ResourceModel\Product;

use Magento\Framework\DB\Select;

class StockStatusBaseSelectProcessor extends \Magento\ConfigurableProduct\Model\ResourceModel\Product\StockStatusBaseSelectProcessor
{
    public function process(Select $select)
    {
        return $select;
    }
}

A Great Solution For Magento 2 Out Of Stock Products

Showing the price of your out-of-stock products isn’t good enough. Yes, customers know you sold that product before, but they don’t know if you will sell it again soon and when you will sell it again.

So what’s the better solution?

The answer is: Magento 2 Out of Stock Notification by BSS

magento-2-out-of-stock-notification

This module allows customers to subscribe to the product they want and notify them immediately when the product is back in stock.

There are many benefits of the Out of Stock Notification feature:

  • Enhances the customer experience in your store: You can turn a negative experience into a positive one.
  • Help analyze hot-selling products based on customers’ demand: Thanks to this, you can increase your revenue.
  • A great way to build relationships with your customers.

Better yet, Magento 2 Out of Stock Notification by BSS doesn’t just bring the basic Out of Stock Notification feature. It also provides many other amazing advanced features.

  • Supports all customer groups, so you can even let your guests or not-logged-in customers subscribe to an out-of-stock product.
  • Allows you to customize your notify button. You can change the text, button text color, or button color of the design easily.
  • Display the notify button on the product page, search result page, and category page. 
  • Enables you to manage customer subscriptions effectively.

We can not mention a unique feature of this extension – the Product Subscription Tab. The Product Subscription Tab is only available to your logged-in customers. This tab allows the customer to check information on products they have subscribed to and stop notification of any product or all products.

Check out its full list of features:

  • Enable stock update notification for guest and not-logged-in customers
  • Automatically send email notify when product is back in stock
  • Allow the admin to manage stock subscription on the Customer Account Page
  • Support product stock alert on many pages (Product Page/Category Page/Search Result Page)
  • Allow stock alert for children product of the configurable products, grouped products and bundle products

Conclusion

In this article, we have shown you the 2 best methods to Magento 2 show price for out of stock products.

We hope this blog is helpful and good luck to you!

BSS Commerce is one of the leading Magento extension providers and web development services in the world. With experienced and certified Magento developers, we commit to bringing high-quality products and services to optimize your business effectively. Furthermore, we offer FREE Installation – FREE 1-year Support and FREE Lifetime Update for every Magento extension.

CONTACT NOW to let us know your problems. We are willing to support you every time.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.