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!

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
scroll top