In Magento 2 how to change a stock availability for 'out of stock' products in frontend (category listing and product detail page). I don't want to override a template file. I want to change a custom message instead of the 'Out of stock' stock availability message in frontend.
layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.form.content">
<block class="Vendor\LowStockNotification\Block\Product" after="product.info.addtocart" template="Vendor_LowStockNotification::custom_file.phtml"></block>
</referenceContainer>
</body>
</page>
layout/catalog_categoy_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="custom.block" template="Vendor_LowStockNotification::custom_file.phtml" before="-" />
</referenceContainer>
</body>
</page>
custom_file.phtml
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$showStockStatus = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('lowstocknotification/low_stock_notification/stock_status/show_stock_status');
$customMessage = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('lowstocknotification/low_stock_notification/stock_status/custom_status');
?>
<!-- custom Stock -->
<input type="hidden" class="show-stock-status" name="showstatus" id="show_stock_status" value="<?php echo $showStockStatus ?>">
<input type="hidden" class="custom-status" name="showstatus" id="custom_status" value="<?php echo $customMessage ?>">
<!-- custom Stock -->
<script type="text/javascript">
require([
'jquery',
'domReady!'
], function ($) {
var showStatus = $('.show-stock-status').val();
var customMessage = $('.custom-status').val();
if(showStatus == 1 && !empty(customMessage)){
$(".stock.unavailable").text(customMessage);
}
});
</script>