How do I set an option in a dropdown attribute as the default? I am adding the options to an existing attribute with this code where $aName is an array of names.

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$aName = array("Name1", "Name2");

$aOption = array();
$aOption['attribute_id'] = $attr_id; 

        $aOption['value']['option'.$iCount][0] = $aName[$iCount];   
        $aOption['order']['option'.$iCount] = $iCount   ;  


I think the answer is in Stackoverflow here but I don't know how to apply it. In my case the Attribute exists, I'm just updating the option list and setting the default.

  • I found the answer for setting the position and included it in my sample code, but I still need to understand how to set an option as the default. – Dan Mar 27 '15 at 14:05

You are using method addAttributeOption($option)

Such method consist following row:

            if (!isset($values[0])) {
                Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));

All appearances the default value will be in $values[0]. Try to look through this method. Most probably you will find more interesting things

|improve this answer|||||

As pointed out by @zhartaunik, you can designate the first option as the default. Another way is to set the default_value in the table eav_attribute:

$attribute = Mage::getModel('eav/attribute')->load($attr_id);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.