如何通过选择下拉属性过滤magento集合? [英] How do I filter a magento collection by a select drop-down attribute?

查看:207
本文介绍了如何通过选择下拉属性过滤magento集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在magento中,我有一个名为cl_designer的属性,它是一个选择下拉选项。我想过滤其上的产品集合,像这样:

In magento, I have an attribute called cl_designer, which is a select drop-down option. I want to filter the products collection on it, like this:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('cl_designer', array('like' => $filter));

但它不工作!当我使用$ collection-> getselect()打印出查询时,我看到它正在比较$ filter和catalog_product_entity_int.value。但这是错误的,因为对于选择选项,catalog_product_entity_int.value是option_id,而不是值。

But it doesn't work! When I print out the query with $collection->getselect(), I see that it is comparing $filter to catalog_product_entity_int.value. But this is wrong, because for select options, catalog_product_entity_int.value is the option_id, NOT the value. So how do I make it filter on the actual option value?

推荐答案

假设一个名为<$ c $的示例下拉属性c> size 包含以下选项:

Assuming an example drop-down attribute named size contains the following options:

id    value
22    'small'
23    'medium'
24    'large'

收集'medium'选项:

按产品(自定义)下拉属性的选项值过滤产品集合:

To filter a product collection by option value of a product's (custom) drop-down attribute:

$sAttributeName = 'size';
$mOptionValue = 'medium';
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter(
        $sAttributeName,
        array(
            'eq' => Mage::getResourceModel('catalog/product')
                        ->getAttribute($sAttributeName)
                        ->getSource()
                        ->getOptionId($mOptionValue)
        )
    );



按下拉选项ID过滤



按产品(自定义)下拉属性的选项ID过滤产品集合:

Filter by drop-down option id

To filter a product collection by a option id of a product's (custom) drop-down attribute:

$sAttributeName = 'size';
$mOptionId = 23;
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter(
        $sAttributeName,
        array('eq' => $mOptionId)
    );

这篇关于如何通过选择下拉属性过滤magento集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆