在 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 过滤产品集合:
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 集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 SELECT(MYSQL/PHP) 中加入 2 个表Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 个表)
如何使<option selected=“selected">由How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 设置?)
使用 PHP 中的数组自动填充选择框Auto populate a select box using an array in PHP(使用 PHP 中的数组自动填充选择框)
PHP SQL SELECT where like search item with multiple wordsPHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(),名称 ASCMySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名称 ASC)