11

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

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

Simply put - use the getAttributeText method.

$product->getAttributeText('brand')
  • This is the right answer. – Owen Sep 27 '16 at 16:50
  • 1
    this was so difficult to find, yet so simple. – Patrick Lee Scott Oct 13 '17 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.