在"is_salable"上过滤产品集合; [英] filtering product collection on "is_salable"

查看:77
本文介绍了在"is_salable"上过滤产品集合;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤产品集合以仅显示有库存的物品.我认为这很容易,因为有一个名为"is_salable"的属性(如果有库存)为1(真),否则为0(假).但是,无论我做什么,都行不通.此外,它似乎在查询结束之前就停止了执行.

I would like to filter a product collection to show only items that are in stock. I thought this would be easy given that there's an attribute called 'is_salable' that is 1 (true) if it's in stock, 0 (false) if not. But no matter what I do, it doesn't work. Further, it seems to halt the execution of the query before it finishes.

以下是一些示例代码:

$this->_productCollection = Mage::getModel('catalog/product')->getCollection();
$this->_productCollection->addAttributeToSelect('*');
$this->_productCollection->addAttributeToFilter('my_attribute', true);
//So far, so good...filtering on 'my_attribute' works!
Mage::Log("select: " . $this->_productCollection->getSelect());
//Successfully outputs the SQL query
$this->_productCollection->addFieldToFilter('is_salable', '1');
Mage::Log("select: " . $this->_productCollection->getSelect());
//does NOT output any query...it's like it died trying

那么我在做什么错了?我已经尝试过'addFieldToFilter','addAttributeToFilter'和其他各种查询,例如:addFieldToFilter('is_salable', array('eq' => true))等...

So what am I doing wrong? I've tried 'addFieldToFilter', 'addAttributeToFilter', and miscellaneous other queries, such as: addFieldToFilter('is_salable', array('eq' => true)), etc...

有人知道该怎么做吗?如果"is_salable"不是答案,那么我所需要做的就是过滤出没有库存的产品...因此,任何可行的方法都可以:)

Anyone know how to do this? If 'is_salable' is not the answer, all I need to do is filter out products that are not in stock...so whatever works to do that would be fine :)

谢谢!

推荐答案

产品中没有is_salable属性,因此它将引发异常.如果只想显示有库存的产品,请使用此库存模型addInStockFilterToCollection方法:

There is no is_salable attribute in the product so it will rise an exception. If you want to display only products that are in stock, use this stock model addInStockFilterToCollection method:

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($this->_productCollection);

这篇关于在"is_salable"上过滤产品集合;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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