2012-05-09 45 views
3

我遇到麻烦使用Magento上的addAttributeToFilter多个选择属性。Magento addAttributeToFilter与多项选择

我有一个多重选择属性的汽车制造,所以内容是'奥迪','宝马'等。在我的例子中,我有一个轮胎产品,我从多重选择中选择了5个。

我想显示产品的制造商'宝马'作为他们选择的领域之一。

我曾尝试以下所有这些都不能正常工作:

$products->addAttributeToFilter('make', array('like' => '%BMW%')); 
$products->addFieldToFilter('make', array('like' => '%BMW%')); 
$products->addAttributeToFilter('make', array('like' => '%38%')); // id of the attribute option 

似乎没有什么工作,我有点卡住了。任何建议非常感谢。

+0

是否$产品 - > addAttributeToFilter('make','38')或$ products-> addAttributeToFilter('make',array('eq',38));给你什么? –

+0

它不起作用。因为它是一个多重选择属性,Magento创建'make'为这样一个字符串:28,30,35,38 - 所以如果我需要过滤宝马的id为38的产品,那么我需要它来检查所有这些值。 –

+0

对不起,错过了! –

回答

10

的解决方案是使用一个Find In Set查询,如:

$this->_productCollection->addAttributeToFilter("make", array("finset"=>"38")); 

的讨论可以在这里看到:http://www.magentocommerce.com/boards/viewthread/201312/

和不同选项的列表:http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections

+0

我发现这也适用于常规下拉式,而不仅仅是多选。我不知道是否有降低速度的定期下拉或不。在ee 1.14.2上测试 –