4

I want to show the price of product, that is "out of stock". Currently it is not shown price for the products in my magento 2.1.3 installation.

How to make visible it?

3
  • Whats the status of product you have set ? is it "out of stock" ? from backend ? – Manthan Dave Jan 30 '17 at 4:59
  • yes, when the product is out of stock, product price is not visible in frontend – Kevin Jan 30 '17 at 8:52
  • make product status from backend as "in stock" and empty quantity field and then check – Manthan Dave Jan 30 '17 at 11:49
3

You need to modify some logic for that. So create a new module and add following code.

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

Vendor/Module/Pricing/Render/FinalPriceBox.php

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;
    }
}
4
  • It helped me and Upvoted. – Jai May 11 '17 at 5:59
  • is there opposite feature - not to show prices of "out of stock" items ? – Gediminas Jun 28 '17 at 7:56
  • The same issue (Website shows products with 0 price and these are the configurable products having all out of stock simple products) happening with me when I upgraded my Magento version from 2.1.0 to 2.1.7 Please someone help – Shailesh Katarmal Jun 30 '17 at 10:33
  • 1
    What about configurable product ? It doesn't show any price. – ahsan Dec 5 '18 at 22:44
6

This one adds to Sohel Rana's answer. Tested on 2.3.3 for configurable products

For any of the product out of stock:

In your di.xml

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

And in \\Pricing\Render\FinalPriceBox.php

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 sub-products out of stock:

In your di.xml

<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;
    }
}
3
  • Your solution works to show price of configurable product, but it doesn't work for price filters. Filters seem to get their data from catalog_product_index_price table which still shows price = 0 for configurable products with out of stock child products. Any idea how to deal with that issue as well? – Lez Nov 23 '20 at 8:13
  • Hello @Lez, I'm having the same issue here did you find a fix ? Thanks ! – magentodev Jan 11 at 9:22
  • @magentodev I tried to solve this by myself, but was not able to track it down completely yet unfortunately. – Lez Jan 11 at 10:19
2

NOPE. 3rd try with different solutions and this also does absolutely nothing. Out of stock items have no price. Any other thoughts on a working solution for Magento 2.2.6

1
  • Well i want find solution too for this... – darkattack Oct 4 '18 at 14:38
1

I just created a small module to show the price of out-of-stock items based on @Cotton 's answer.

Feel free to fork: https://github.com/nordcomputer/magento2-showoutofstockprice

Composer install should work if you add the repo to your dependencies. Be aware, that I set the required PHP version to ^7.1 - you may need to reconfigure that in the composer.json file if you use another version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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