1

Even if Magento 2.2.5 Configrable Product is out of stock, I want to display the item price.

How can I see the price?

1 Answer 1

1

If you want to display out of stock Configurable Product price then need to override Magento\Catalog\Pricing\Render\FinalPriceBox.php file and change the code in protected function _toHtml() for display out of stock Configurable Product price.

Follow below file path.

File path: magento/app/code/Vendor/OutofstockProductPrice/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_OutofstockProductPrice',
    __DIR__
);

File path: magento/app/code/Vendor/OutofstockProductPrice/etc/module.xml

<?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="Vendor_OutofstockProductPrice" setup_version="1.0.0" />
</config>

File path: magento/app/code/Vendor/OutofstockProductPrice/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\OutofstockProductPrice\Pricing\Render\FinalPriceBox" />
    <preference for="Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox"
        type="Vendor\OutofstockProductPrice\Pricing\Render\FinalPriceBox" />
</config>

File path: magento/app/code/Vendor/OutofstockProductPrice/Pricing/Render/FinalPriceBox.php

<?php
namespace Vendor\OutofstockProductPrice\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;
    }
}

Run below command:

php bin/magento setup:upgrade

php bin/magento cache:flush

1
  • The price is displayed. But it is unnatural that the price becomes zero. Commented Aug 23, 2019 at 7:06

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.