Reputation: 81
I have a question about the product category view. As I have out of stock products showing (Needs to be like this as we are a wholesaler) I would like it displayed so that the customer can see if it is in stock before drilling down into the detailed product page.
Please see attached what part I would like to see on the category page:
Link to image of idea: http://ipseitycore.co.uk/screen.jpg
Upvotes: 1
Views: 8523
Reputation: 11
Use this code in list.phtml file
<?php $ObjectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $ObjectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
echo $StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());
?>
Upvotes: 1
Reputation: 700
Try the line of code below in the list template (catalog/product/list.phtml):
<?php
$qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo $qty;
?>
Hope this helps.
Upvotes: 2
Reputation: 46
I actually had this code added to just show "Out of stock" or "Only 1 left". For my website, I didn't need "In Stock" to show.
<span class="out-of-stock">
<?php
$qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
if ($qty == 1) :
echo $this->__('Only 1 left in stock!');
elseif ($qty == 0) :
echo $this->__('Out of stock!');
else :
echo $this->__(' ');
endif;
?>
</span>
Upvotes: 1