0

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.

1 Answer 1

1

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.

3
  • No, still not working. Jul 1, 2014 at 14:04
  • You may set multiple default options..make sure about this ..
    – Elavarasan
    Jul 1, 2014 at 14:18
  • I know what's wrong. When I create product default value is set correctly. When I edit product that already exists - value is set to "No" which is last option in select. I have to update all product values for this column. Jul 1, 2014 at 14:30

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.