2
votes

I am using Magento 1.9. I want to show "Minimum Qty Allowed in Shopping Cart" in list.phtml page. For example I have set minimum quantity "6" in Product attribute then it should show "6" in front end. I am trying to write this code but it is throwing an error. Maybe I am writing a wrong code.

 <?php 
$productQuantity = Mage::getModel("cataloginvetory/stock_item")->loadByProduct($_product->getId());
echo $productQuantity->getMinSaleQty(); ?>

I am getting following error

Fatal error: Call to a member function loadByProduct() on a non-object in //list.phtml

3

3 Answers

2
votes

Here some logic

<?php                 
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
                 echo $stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0 ? $stockItem->getMinSaleQty() * 1 : null;
 ?>
0
votes

Mage::getModel("cataloginvetory/stock_item") is not returning an object. Use var_dump() to find out what Mage's static function is returning.

echo var_dump(Mage::getModel("cataloginvetory/stock_item"));

I presume it will return with NULL or false. If that is the case something is going wrong in getModel() (probably incorrect parameters)

0
votes

There are two mistakes that I see right off the bat.

First, "cataloginvetory/stock_item" is a typo. It should be "cataloginventory/stock_item."

Second, loadByProduct takes a product - not a productId.

It should look like:

<?php 
$productQuantity = Mage::getModel("cataloginventory/stock_item")->loadByProduct($_product);
echo $productQuantity->getMinSaleQty(); ?>

I wasn't able to test the getMinSaleQty() function because I don't have that enabled on my site.