Get a list of attribute options from Magento
Solution 1
It looks like that you initialize attribute by yourself, instead of using Magento attribute initialization process:
Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode)
Because since 1.4.x Magento has separate attribute models for catalog and customers model and definition of default source model for catalog_product
now is moved from EAV attribute model (Mage_Eav_Model_Entity_Attribute
) to the catalog one (Mage_Catalog_Model_Resource_Eav_Attribute
).
As a result, some catalog attributes won't work with the EAV attribute model. Particularly those that use Mage_Eav_Model_Entity_Attribute_Source_Table
but don't explicitly define it (color, manufacturer, etc.).
The following code snippet should work perfectly on your installation:
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
By the way Mage_Eav_Model_Config
model has a lot of helpful methods, that can be used in your development, so don't hesitate to look into this model.
Solution 2
The above code does not work if the resource_model is empty. The following snippet does the job:
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'YOUR_ATTRIBUTE_CODE');
/** @var $attribute Mage_Eav_Model_Entity_Attribute */
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getId())
->setStoreFilter(0, false);
Solution 3
$attribute = Mage::getModel('eav/config')->getAttribute('customer','cateinterest');
$options = $attribute->getSource()->getAllOptions();
Solution 4
<?php
//Possible color value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
$id = $instance['value']; //id of the option
$value = $instance['label']; //Label of the option
Chris Forrette
I'm a full stack developer from Portland, OR, working a lot with Node and Elasticsearch nowadays, a little bit of React, and with plenty of experience with Python/Django, HTML/CSS, etc.
Updated on July 09, 2022Comments
-
Chris Forrette almost 2 years
I've been grabbing attribute options from Magento like so:
<?php if ($attribute->usesSource()) { $options = $attribute->getSource()->getAllOptions(false); } ?>
It's been working fine until I tried to get the options for the built in 'color' attribute -- I got the following error:
PHP Fatal error: Call to a member function setAttribute() on a non-object in app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php on line 374
It would appear that the
getSource()
call fails and causes this error. Does anyone know why this happens and how I can get color options out?Thanks!
-
Thiem Nguyen almost 12 yearsplease add more details on your answer
-
CarComp about 10 yearsThis works for eav attributes that don't populate using the above methods
-
Ricardo Martins almost 10 yearsThanks. In add to the answer, when dealing with 'catalog_product' it uses Mage_Eav_Model_Entity_Attribute_Source_Table class and the parameters are: getAllOptions($withEmpty = true, $defaultValues = false). Thank you one more time.
-
ahe_borriglione almost 8 yearsPlease consider that this solution doesn't take care about the attribute values sort order. $attribute->getSource()->getAllOptions(false) does
-
barbsan about 5 yearsCould you edit your answer and add some explanation why/how your code solves the issue?