在“is_salable"上过滤产品集合 [英] filtering product collection on "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屋!