【问题标题】:adding a multiple list attribute to product collection filter in magento在 magento 中向产品集合过滤器添加多个列表属性
【发布时间】: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


    【解决方案1】:

    请试试这个..

    $collection = Mage::getModel('catalog/product')->getCollection();
        ->addAttributeToSelect('*')
        ->addFieldToFilter(
            'my_attribute_id',
            array(
                'like' => Mage::getResourceModel('catalog/product')
                            ->getAttribute('my_attribute_id')
                            ->getSource()
                            ->getOptionId($searched)
            )
        );
    

    【讨论】:

    • 感谢您的回复...我添加了您的建议,但没有用..我忘了在原始帖子中提到我还添加了以下代码:$productCollection->addAttributeToSelect(Mage ::getSingleton('catalog/config')->getProductAttributes());无论如何,我用你的建议替换了我的代码,但仍然没有..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2011-04-30
    • 2013-01-05
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多