【发布时间】:2013-02-12 09:01:09
【问题描述】:
我正在尝试进行与 ajax 相关的 magento 搜索,但我无法将多选类型产品属性正确添加到产品集合中, 例如:
$productModel = Mage::getModel('catalog/product'); //getting product model
$productCollection = $productModel->getCollection();
$productCollection->addAttributeToSelect(
Mage::getSingleton('catalog/config')
->getProductAttributes()
);
$productCollection->addAttributeToFilter(
array(
array('attribute'=>'my_attribute_id',
'finset' => Mage::getResourceModel('catalog/product')
->getAttribute('my_attribute_id')
->getSource()
->getOptionId($searched))
);
其中 $searched 是我保存关键字的字符串。现在,让我们假设 my_attribute_id 是一个多选产品属性,它有一个名为“Red Bull”的选项......如果我搜索确切的字符串“red Bull”,它可以工作,但如果我搜索,我想工作仅在“红色”或“公牛”之后。 即使搜索字符串不完整,有没有办法获取属性的选项 ID?因为问题就在这里:
Mage::getResourceModel('catalog/product')
->getAttribute('my_attribute_id')
->getSource()
->getOptionId($searched))
只有当我完全搜索它时,此代码才会返回属性选项的 id。可能模型会执行类似这样的查询
"select...where value='$searched'"
即使选项的值不完整,是否有办法获取属性选项 id 的列表?..so 进行这样的查询
"select...where value like '%$searched%'"
或者有没有更好的方法在多选属性部分值之后检索产品集合,而不是我正在尝试的解决方案? 非常感谢!
【问题讨论】:
标签: magento collections attributes multi-select