Magento – Creating custom attribute programmaticaly default value

defaultdropdown-attributemagento-1.8product-attribute

How to set default value to "Yes" in generated select element? I created installation script and attribute is created perfectly. But on create/edit page of product I get No selected attribute for option.

<select class=" select" name="product[abroad_shipping]" id="abroad_shipping">
<option value="1">Yes</option>
<option selected="selected" value="0">No</option>
</select>



<?php

/* @var $this Mage_Core_Model_Resource_Setup */

$installer = $this;
$installer->startSetup();

/* @var $setup Mage_Eav_Model_Entity_Setup */
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId = $setup->getEntityTypeId('catalog_product');
$setup->removeAttribute($entityTypeId, 'abroad_shipping');

$attributeData = array(
    'type' => 'int',
    'input' => 'select',
    'label' => 'Possible abroad shipping',
    'source' => 'eav/entity_attribute_source_boolean',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'required' => false,
    'user_defined' => true,
    'default_value' => '1',
    'default' => array('1'),
//  'default' => '1',
//    'option' => array(
//        'values' => 
//        array(
//            0 => 'Yes',
//            1 => 'No'
//        )
//        ),
);

$setId = $setup->getAttributeSetId($entityTypeId, 'my_custom_set');
$groupId = $setup->getAttributeGroupId($entityTypeId, $setId, null);
$setup->addAttribute($entityTypeId, 'abroad_shipping', $attributeData);
$setup->addAttributeToGroup($entityTypeId, $setId, $groupId, 'abroad_shipping', 90);

$installer->endSetup();

I tried default, default value, integer default, string default, array. Nothing worked.

Best Answer

default is the correct method for setting default values for the drop down, For example, My custom options are like,

Myoption-1
Myoption-2
Myoption-3
Myoption-4
Myoption-5

And your installer script should be like this,

$installer->addAttribute("customer", "mydropdown",  array(
    "type"     => "int",
    "backend"  => "",
    "label"    => "My Drop down",
    "input"    => "select",
    "source"   => "mymodule/eav_entity_attribute_source_customeroptions14042221240",
    "visible"  => true,
    "required" => false,
    "default" => "2",
    "frontend" => "",
    "unique"     => false,
    "note"       => "This is My Drop down option"

    ));

Here the default having values is 2. That means Myoption-2 is the default option. You may be entered wrong value. check the value of that option that you need to set as a default.

Related Topic