Question

The below code works well for products who's stock is being managed, but it displays "0" if "Manage Stock" is selected as "No" in the Magento backend. There's probably at least a few hundred products where the stock isn't currently managed, so going through them all isn't feasible. So, is it possible, when the product's stock isn't managed, to swap the "0" it places there with some sort of text like "call us about inventory"?

This is in app/design/frontend/{theme}/default/template/catalog/product/view/type/default.phtml :

<?php if ($_product->isAvailable()): ?>
<p class="availability in-stock"> <span class="fa fa-check"> </span> <?php echo $this->__('Qty Available:') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?></span></p>
<?php else: ?>
    <p class="availability out-of-stock"> <span class="fa fa-times"> </span> <?php echo $this->__('') ?> <span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

Some extra context: The "0" appears because Manage Stock is set to "No", Qty is set to "0", and the Stock Availability set to "In Stock". Even though the Manage Stock is set to "No", the code above still seems like it ignores that & grabs whatever is in the Qty field anyway.

Was it helpful?

Solution

Use following code:

<?php if ($_product->isAvailable()): ?>
    <p class="availability in-stock"> <span class="fa fa-check"> </span> <?php echo $this->__('Qty Available:') ?> <span><!--<?php echo $this->__('In stock') ?>-->
    <?php $manageStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId())->getManageStock();
     if($manageStock == 0) { 

     echo 'Call us about inventory';
     }else{
     ?>

    <?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?></span>
    <?php } ?>
    </p>
    <?php else: ?>
        <p class="availability out-of-stock"> <span class="fa fa-times"> </span> <?php echo $this->__('') ?> <span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top