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