Magento issue with calling isInStock() method on a product

前端 未结 3 1396
面向向阳花
面向向阳花 2020-12-29 10:05

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 w

3条回答
  •  时光取名叫无心
    2020-12-29 10:30

    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!
    }
    

提交回复
热议问题