I want to check if some products are in stock but whatever I do the isInStock() method always returns TRUE. My products are configurable products with no associated products and under the "Inventory" tab "Stock Availability" is set to "Out of Stock". What am I doing wrong? Thanks!
+1
A:
Magento has a lot of history at this point, so it's a good idea to not always trust that method names will do what "seems obvious". Obvious now wasn't obvious a few years ago.
If you look at the following two methods on the Mage_Catalog_Model_Product class
public function isInStock()
{
return $this->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_ENABLED;
}
public function getStatus()
{
return $this->_getData('status');
}
You can see that isInStock
checks the status attribute, set in the "General" section of the Product admin.
Try this instead
$stockItem = $product->getStockItem();
if($stockItem->getIsInStock())
{
//in stock!
}
else
{
//not in stock!
}
Alan Storm
2010-04-24 08:45:10
That was it! Thanks Alan!
Nick Dima
2010-04-24 13:07:45