How can Magento find an attribute value by a given attribute label or a given attribute id?
-
I believe this is answered by another SO question seen [here]( magento.stackexchange.com/a/8396)– sbditto85Jun 9, 2015 at 21:53
-
3Possible duplicate of How to get attribute "option label/attribute text" having "attribute value" (option_id)?– Teja Bhagavan KolleparaJan 12, 2017 at 11:10
3 Answers
$productModel = Mage::getModel('catalog/product');
$str_attr_label = "color"; //or "size", etc...
$int_attr_id = 8; // or any given id.
$int_attr_value = 21; // or any given attribute value id.
// Chose either
if ($byLabel){
$attr = $productModel->getResource()->getAttribute($str_attr_label);
}
if ($byId){
$attr = Mage::getModel('catalog/resource_eav_attribute')->load($int_attr_id);
}
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText($int_attr_value);
}
Simply put - use the getAttributeText method.
$product->getAttributeText('brand')
-
-
1
In case anyone finds this page and wants some down low methods of looking up attributes of any kind, instead of just product attributes, here is an example to look up a random attribute I created that is called 'specialty' and list all the options as an array.
$attr = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('specialty')->getData()[0];
$attributeModel = Mage::getModel('eav/entity_attribute')->load($attr['attribute_id']);
$src = $attributeModel->getSource()->getAllOptions();