Magento: Getting product attributes values and labels

I have found that it is very useful to be able to get attributes from the system and use them in places other than a products category page. I always forget the exact syntax to use so, this is going to be my unofficial cheat sheet.

This is how to get a drop down lists options. I don’t think it will work for a mulit-select attribute. I stick the value/label pairs into an array to use how I please.

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attributeArray[$option['value']] = $option['label'];

I had a trickier time getting values for a multi-select attribute. I don’t think that this is the best method, but it is one that worked for me. First the multi-select attribute must be set to be used in the advanced search. You can set this in the manage attributes area.

$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();
foreach($attributes as $a){
if($a->getAttributeCode() == 'desired_attribute_code'){
foreach($a->getSource()->getAllOptions(false) as $option){
$attributeArray[$option['value']] = $option['label'];

Here is a better way to get the multi-select attribute values.

$attributeId = Mage::getResourceModel('eav/entity_attribute')
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();

If you only need to retrieve a value for a product you can try this way

//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443

I added this code snip it below to possibly answer a question that was posted. The code below will get an attribure collection. Set {entityType} to 4 to get attributes for products. You can remove the “setCodeFilters” line if you want to get all the attributes. To get anything really useful you will probably need to get the resulting attribute ids and do someting with them, like use them in a filter for the products collection or something.

$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')

Entity Type Id’s
$entityType is an integer id for what type of entity the attribute is associated to. If you look at the “eav_attribute” table you will see that each attribute has an entity_type_id.
1 = Customer Entity
2 = Shipping Entity (I believe)
3 = Category Entity
4 = Product Entity

The Following code was added to answer a question in the replies:
To make a product attribute avaliable when getting a product collection(such as on a category page or search results page) you can add some code the a config.xml file that instructs Magento to load allways the attribute when a product collection is loaded.

How to add an attribute to be loaded whenever a product collection is loaded:
This can go in any config.xml file. I would recommend putting it in a custom module rather than one from the core code. Just replace “attribute_name” with the attribute code of the attribute you are trying to add.

How to get Only the Attribute Values that have been used on products
The Code below will get the attribute values that have been used on the products in the product collection.
This may not be the best way to do this, but it is what I came up with.

//First get the attribute id
$audienceAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','session_audience');
//Now Load the attribute
$audienceAttribute = Mage::getModel('catalog/resource_eav_attribute')->load($audienceAttributeId);

//Now get the product collection that you want to use to fine the attributes values for.
//I wanted the attribute values for only grouped products. You could add category filters and such
$productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSort('session_audience', 'asc')
->addAttributeToFilter('type_id', array('eq' => 'grouped'));

//Now get the product ids for the collection
$productCollectionIds = $productCollection ->getAllIds();

//Now we query the data base to get the attribute values for the given product ids.
//NOTE that I am selecting from the catalog_product_entity_varchar table because this is the type of attribute I was using.
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $read->select();
->where('attribute_id = ?', $audienceAttributeId)
->where('entity_id IN (?)', $productCollectionIds );


//Now get the ids for the attribute values.
$result = $read->query($select);
$attributeOptionIds = array();
while($row = $result->fetch()){
$attributeOptionIds = array_merge($attributeOptionIds, explode(',', $row['value']));

//Now get the actual values for the Ids as a Collection and do something with the values.
$filteredAudienceCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->addFieldToFilter('main_table.option_id', array('in' => $attributeOptionIds))