0

Store Is set up to allow backorders on some products. When these items are out of stock they still show as 'In Stock' on the product page but the user gets notified when they visit the cart that the item is on backorder.

I would like to change the product page so it also shows backorder available in place of the 'In Stock' text for these items.

0

You can add condition as below in In magento 2 how to override stock availability in product detail page in custom module and add backorder message in template file

enter image description here

app/code/Vendor/Module/view/frontend/layout/catalog_product_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="product.info.type">
        <block class="Magento\Catalog\Block\Product\View\Type\Simple" name="product.info.simple" as="product_type_data" template="Vendor_Module::product/view/type/default.phtml"/>
    </referenceContainer>
</body>
</page>

app/code/Vendor/Module/view/frontend/templates/product/view/type/default.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product = $block->getProduct() ?>

<?php if ($block->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable()): ?>        
            <p class="stock available"><?php echo $this->__('Availability:') ?>
                    <span><?php echo $this->__($_product->isInStock() ? 'In stock' : 'On Backorder') ?></span></p>
    <?php else: ?>
        <div class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
            <span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
        </div>
    <?php endif; ?>
<?php endif; ?>
3
  • Thanks for your answer but it doesn't work.
    – Shaun
    Dec 4 '21 at 13:16
  • Then just try to change some logic..instead of checking isinstock ...check with is backorder enable and qty is less then zero Dec 4 '21 at 19:15
  • Can you look at what I'm doing wrong.
    – Shaun
    Dec 5 '21 at 2:40
0

What am I doing wrong. Shows out of stock no matter what the qty or status.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product_inventory = $block->getProduct() ?>

<?php if ($block->displayProductStockStatus()) :?>
    <?php if($_product_inventory->getQty() <= 0 && ($_product_inventory->getBackorders() == 1 || $_product_inventory->getBackorders() == 2)): ?> 
        <div class="stock available" title="<?php echo $this->__('Availability:') ?>">
            <span><?php echo $this->__('Backorder Available') ?></span>
        </div>
    <?php else :?>
        <div class="stock unavailable" title="<?= $block->escapeHtmlAttr(__('Availability')) ?>">
            <span><?= $block->escapeHtml(__('Out of Stock')) ?></span>
        </div>
    <?php endif; ?>
<?php endif; ?>

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.