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!
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!
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With