0

我正在尝试进行与 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%'"

或者有没有更好的方法在多选属性部分值之后检索产品集合,而不是我正在尝试的解决方案?非常感谢!

4

1 回答 1

0

请试试这个..

$collection = Mage::getModel('catalog/product')->getCollection();
    ->addAttributeToSelect('*')
    ->addFieldToFilter(
        'my_attribute_id',
        array(
            'like' => Mage::getResourceModel('catalog/product')
                        ->getAttribute('my_attribute_id')
                        ->getSource()
                        ->getOptionId($searched)
        )
    );
于 2013-02-12T11:21:28.553 回答