1

In recently viewed products, the price is not showing for Out of Stock products.

I have to show Out Of Stock products as client requirement.

enter image description here

2
  • Sorry, I don't really understand the question. So you want the price to display for out of stock products?
    – Greg
    Commented Sep 19, 2018 at 12:07
  • @BareFeet Yes, i have to render price for out of stock products. Commented Sep 19, 2018 at 12:12

2 Answers 2

2

Best option would be creating your own custom module to add custom code. Call it CustomModule_AddPriceToOutOfStock

Now, create a directory in app/code Add registration.php file at app/code/CustomModule/AddPriceToOutOfStock/registration.php

With following code:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'CustomModule_AddPriceToOutOfStock',
    __DIR__
);

You also need to create module.xml at app/code/CustomModule/AddPriceToOutOfStock/etc/module.xml

With following code:

<?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="CustomModule_AddPriceToOutOfStock" setup_version="2.0.0">
    </module>
</config>

Now create di.xml at app/code/Custom/Outofstock/etc/di.xml

With below 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="CustomModule\AddPriceToOutOfStock\Pricing\Render\FinalPriceBox" />
</config>

Now, create Price render file FinalPriceBox.php at app/code/CustomModule/AddPriceToOutOfStock /Pricing/Render/FinalPriceBox.php

With below code:

<?php
namespace CustomModule\AddPriceToOutOfStock\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;
    }
}

Enable the module using:

php bin/magento setup:upgrade

And flush the cache:

php bin/magento cache:flush

Magento 2 : How To Show Price of "out of stock" Products

12
  • I have tried this but still, it was not coming. Commented Sep 19, 2018 at 12:30
  • I just tired it myself, and it did work for me..
    – Greg
    Commented Sep 19, 2018 at 12:34
  • ok trying again Commented Sep 19, 2018 at 12:36
  • No need to down-vote tho haha :D But okey, I hope it works for you, I use this myself with no problems. Also, do you get any errors?
    – Greg
    Commented Sep 19, 2018 at 12:44
  • 1
    Also, what version of Magento are you using?
    – Greg
    Commented Sep 19, 2018 at 12:49
0

It's using KO js to generate the price box for recently viewed products, so you can following the steps below: Add 2 files to your theme Magento_Catalog/web/template/product/price/regular_price.html

<if args="getRegularPrice($row())">
    <span css="'old-price': hasSpecialPrice($row()), 'regular-price': !hasSpecialPrice($row())">
        <span class="price-container"
              css="getAdjustmentCssClasses($row())">
            <span if="label && hasSpecialPrice($row())"
                  class="price-label"
                  text="label"/>

            <span class="price-wrapper"
                  css="priceWrapperCssClasses"
                  attr="priceWrapperAttr"
                  data-price-amount=""
                  data-price-type=""
                  html="getRegularPrice($row())"/>

            <if args="!hasSpecialPrice($row())">
                <each args="data: getAdjustments(), as: '$adj'">
                    <render args="$adj.getBody()"/>
                </each>
            </if>
        </span>
    </span>
</if>

Magento_Catalog/web/template/product/price/special_price.html

<if args="hasSpecialPrice($row())">
    <span class="special-price">
        <span class="price-container"
              css="getAdjustmentCssClasses($row())">
            <span if="label"
                  class="price-label"
                  text="label"/>

            <span class="price-wrapper"
                  css="priceWrapperCssClasses"
                  attr="priceWrapperAttr"
                  data-price-amount=""
                  data-price-type="finalPrice"
                  html="getPrice($row())"/>

            <each args="data: getAdjustments(), as: '$adj'">
                <render args="$adj.getBody()"/>
            </each>
        </span>
    </span>
</if>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.