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

查看:27
本文介绍了如何通过选择下拉属性过滤 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?

推荐答案

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

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

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

并且您想通过 'medium' 选项过滤您的收藏:

and you want to filter your collection by 'medium' options:

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

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天全站免登陆