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
php bin/magento setup:upgrade
command to activate your module