0

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');
$installer->startSetup();
$aName = array("Name1", "Name2");

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

for($iCount=1;$iCount<sizeof($aName);$iCount++){
        $aOption['value']['option'.$iCount][0] = $aName[$iCount];   
        $aOption['order']['option'.$iCount] = $iCount   ;  
}

$installer->addAttributeOption($aOption);
$installer->endSetup();

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
0

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

0

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);
$attribute->setDefaultValue($default);
$attribute->save();

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.