Get the array of all attribute options in Magento

11.Oct.2012

Please check out updated version of this snippet.

Suppose you want to display all the attributes of one product. You can easily get the id of the value by call the unversal method

1
$attributeValueId = $_product->getResource()->getColor();

where color is the name of the attribute. If you’ve already loaded the whole product into the variable $_product then you don’t need the getResource() part. Also, attributes should be named with lowcase letters, and their respectful methods are formed with the prefix get and capital first letter, but if you have two-word named attribute (for example my_attribute – remember, no whitespaces in the object names in Magento!) then you could get it with the method getMyAttribute() which follows the standard Zend naming convention.

Now, in order to display je value meaningful to the user, and not just the id, you can fetch all the values into the associative array

1
2
3
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
      foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) {
            $myArray[$instance['value']] = $instance['label'];

and simply use it in your display!

4
5
6
7
8
$_product->load($idOfTheProduct);
 
$attributeValueId = $_product->getColor();
 
echo 'This shirt is ' . $myArray[$attributeValueId];