Assuming an example drop-down attribute named size
contains the following options:
id value
22 'small'
23 'medium'
24 'large'
and you want to filter your collection by 'medium'
options:
Filter by drop-down option value
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)
)
);
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)
);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…