Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento issue with calling isInStock() method on a product

Tags:

php

magento

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!

like image 687
Nick Dima Avatar asked Apr 23 '10 19:04

Nick Dima


1 Answers

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!
}
like image 156
Alan Storm Avatar answered Nov 11 '22 15:11

Alan Storm