3

Here's how I create a new Product multiselect attribute:

$eav = new Mage_Catalog_Model_Resource_Setup('core_setup');

$eav->addAttribute(
    Mage_Catalog_Model_Product::ENTITY, 
    "product_country", 
    array(
        'label'                      => 'Country',
        'group'                      => 'General',
        'type'                       => 'text',
        'input'                      => 'multiselect',
        'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
        'user_defined'               => true,
        'required'                   => true,
        'visible'                    => true,
        'source'                     => 'ns/some_source', // this source has the "UK" value
        'backend'                    => 'eav/entity_attribute_backend_array',
        'default'                    => 'UK',
    )
);

I've also tried with "Yes/No" values using

"type" => "boolean" 

or

"type" => "select",
'source' => 'eav/entity_attribute_source_boolean'

which are functionally identical.

In all cases, having a default key with value correctly populates the eav_attribute table, column default_value. But having "default" => "1" for "Yes/No" attribute doesn't do anything as far as the input on the edit page is concerned. "No" is still selected, and I was expecting "Yes", because "1" is mapped to "Yes":

// Mage_Eav_Model_Entity_Attribute_Source_Boolean
$this->_options = array(
    array(
        'label' => Mage::helper('eav')->__('Yes'),
        'value' =>  1
    ),
    array(
        'label' => Mage::helper('eav')->__('No'),
        'value' =>  0
    ),
);

The same thing happens for multiselect: no option is selected by default.

I'm out of ideas. Does anyone know what's the purpose of the "default" column/key, if not setting a default value of an attribute? How can one set a value of an attribute to be automatically selected on new/edit product backend page?

dotancohen
  • 30,064
  • 36
  • 138
  • 197
nevvermind
  • 3,302
  • 1
  • 36
  • 45

1 Answers1

2

I encountered this problem also, there's an issue when creating custom {product, customer, address} attributes and adding entities.

In Mage_Catalog_Model_Resource_Setup::_prepareValues some 'default' entity set is defined which causes this troubles.

The best solution for this is loading the attribute after creating the attribute and set the default value.

 $model = Mage::getModel('eav/entity_attribute')
     ->load($installer->getAttributeId(Mage_Catalog_Model_Product::ENTITY, 'product_country'));
 $model
     ->setDefaultValue(Mage::helper('eav')->__('Yes'))
     ->save();
Jeffrey de Graaf
  • 479
  • 3
  • 17