12

How can Magento find an attribute value by a given attribute label or a given attribute id?

2

3 Answers 3

17
$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);
}       
0
11

Simply put - use the getAttributeText method.

$product->getAttributeText('brand')
2
  • This is the right answer.
    – Owen
    Sep 27, 2016 at 16:50
  • 1
    this was so difficult to find, yet so simple. Oct 13, 2017 at 15:15
2

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.