Magento: Get Attribute Id from Option Id

I am working on a module, and ran into a problem where I needed to be able to get the frontend name of an attribute, while only having the option id for one of the many options of an attribute (dropdown or multiselect). Getting this turned out to be easier than I thought:

$_attributeId = Mage::getModel('eav/entity_attribute_option')->load($optionId)->getAttributeId();
$_attribute = Mage::getModel('eav/entity_attribute')->load($_attrId);
echo $attribute->getFrontendLabel();

The above code gets the attribute id for the particular option. Then it loads up the attribute model for that particular attribute, and gives you access to any of the available data, allowing you to call the getFrontendLabel() method to get the attribute name.

This entry was posted in Magento. Bookmark the permalink.

3 Responses to Magento: Get Attribute Id from Option Id

  1. Cristian says:

    Very usefull!!! Very thanks!

  2. Darius says:

    ..this snippet helped me 😉

    There are three errors in the Code – you mixed up the names of the variables.
    Thank`s anyway!

  3. iain says:

    Hi, should line 2 load($_attrId) be load($_attributeId) ? if not, where is $_attrId being set?