1

I'm pretty new to Magento in general and just started working with Magento 2.

Here is my question - How to show prices for all out of stock products?

There is this topic: Magento 2 : How To Show Price of "out of stock" Products

And apparently it works, but half of the instructions don't make sense to me. After creating custom module, what should I do with it? Should I change anything else anywhere?

Thanks!

5
  • Did you enable your module?
    – Abbas
    Commented Feb 16, 2017 at 2:09
  • Good question. I'm not sure how to do it :) Commented Feb 16, 2017 at 2:21
  • run php bin/magento setup:upgrade command to activate your module Commented Feb 16, 2017 at 4:51
  • Possible duplicate of Magento 2 : How To Show Price of "out of stock" Products Commented May 18, 2017 at 4:19
  • Fatal error: Uncaught RuntimeException: Can't create directory /var/www/html/generated/code/Magento/Directory/Helper/Data/. in [Magento\Directory\Helper\Data\Proxy] in /var/www/html/vendor/magento/framework/Code/Generator.php:115 Stack trace: #0 /var/www/html/vendor/magento/framework/Code/Generator/Autoloader.php(35): Magento\Framework\Code\Generator->generateClass('Magento\\Directo...') #1 [internal function]: Magento\Framework\Code\Generator\Autoloader->load('Magento\\Directo...') #2 [internal function]: spl_autoload_call('Magento\\Directo...') #3 /var/www/html/vendor/magento/framework/Objec
    – vagg
    Commented Aug 29, 2018 at 13:46

1 Answer 1

7

You need to create your custom module using below code

Let say module name Custom_Outofstock

So create folder in app/code

Create registration.php file at app/code/Custom/Outofstock/registration.php

Add below code to it:

<?php

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

Create module.xml at app/code/Custom/Outofstock/etc/module.xml

Add below code to it:

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

Now create file suggested in this answer given By Sohel Rana as below

Create di.xml at app/code/Custom/Outofstock/etc/di.xml

Add below code to it:

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

Create Price render file FinalPriceBox.php at app/code/Custom/Outofstock/Pricing/Render/FinalPriceBox.php

Add below code to it:

<?php
namespace Custom\Outofstock\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;
    }
}

Now run below command to enable module:

php bin/magento setup:upgrade

Flush cache using below command:

php bin/magento cache:flush
5
  • Thanks for the answer! However, after doing this, all the css was overwritten with default css which came with theme. And products are not displaying whatsoever, instead I get this message: "We're sorry, an error has occurred while generating this email". In exception log i have this message: "[2017-02-13 17:11:58] main.CRITICAL: exception 'Magento\Framework\Exception\LocalizedException' with message 'The element 'product.info.options' already has a child with alias 'default'' in /home/uspatriot/public_html/vendor/magento/framework/Data/Structure.php:611" Any ideas what happened? Commented Feb 16, 2017 at 18:16
  • Actually, after enabling developer mode, then disabling it, and then enabling production mode, this is the error I get now: "Fatal error: Class 'Vendor\Module\Pricing\Render\FinalPriceBox' not found in /home/uspatriot/public_html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93" Commented Feb 16, 2017 at 18:22
  • So, I figured there was a mistake in di.xml. After changing type from "Vendor\Module\Pricing\Render\FinalPriceBox" to "Custom\Outofstock\Pricing\Render\FinalPriceBox", and re-enabling production mode, it all works now! Commented Feb 16, 2017 at 18:45
  • Yes that is error I forgot to modify it. I have updated code thanks for acknowledge Commented Feb 17, 2017 at 5:45
  • Is there any modification to show "as low as" price only of "in stock" products on configurable product? magento.stackexchange.com/questions/181053/…
    – Gediminas
    Commented Jun 28, 2017 at 13:51

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.