Get a list of attribute options from Magento

Viewed 59515

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!

5 Answers
<?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
Related