問題描述
我想檢查一些產品是否有庫存,但無論我做什么,isInStock()
方法總是返回 TRUE
.我的產品是沒有關聯產品的可配置產品,在庫存"選項卡下,庫存可用性"設置為缺貨".我究竟做錯了什么?謝謝!
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 在這一點上有很多歷史,所以最好不要總是相信方法名稱會做看起來很明顯"的事情.現在明顯在幾年前并不明顯.
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.
如果你在 Mage_Catalog_Model_Product 類上查看以下兩個方法
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');
}
您可以看到 isInStock
檢查了 status 屬性,該屬性在產品管理員的常規"部分中設置.
You can see that isInStock
checks the status attribute, set in the "General" section of the Product admin.
試試這個
$stockItem = $product->getStockItem();
if($stockItem->getIsInStock())
{
//in stock!
}
else
{
//not in stock!
}
這篇關于在產品上調用 isInStock() 方法的 Magento 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!