Magento问题,在产品上调用isInStock()方法 [英] Magento issue with calling isInStock() method on a product

查看:77
本文介绍了Magento问题,在产品上调用isInStock()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查某些产品是否有库存,但是无论我做什么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检查了在产品管理员的常规"部分中设置的状态属性.

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

这篇关于Magento问题,在产品上调用isInStock()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆