Question

I am trying to add a multiselect by my install script. This is my code:

$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();

$options = array();
foreach(Mage::getModel('adminhtml/system_config_source_country')->toOptionArray() as $option) {
    $options["value"][$option["value"]] = array($option["label"]);
}

$installer->addAttribute('catalog_product', 'allowed_countries', array(
    'group'                         => 'General',
    'type'                          => 'varchar',
    'input'                         => 'multiselect',
    'label'                         => 'Restrict shipping to',
    'backend'                       => 'eav/entity_attribute_backend_array',
    'visible'                       => 1,
    'required'                      => 0,
    'user_defined'                  => 1,
    'configurable'                  => 1,
    'global'                        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'option'                        => $options
));

$installer->endSetup();

The problem is that the values of the options are not the values of the array $options.

For example: Magento changes "US" => "United States" to "8396" => "United States" enter image description here

Any ideas?


Here is the solution:

$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();

$installer->addAttribute('catalog_product', 'allowed_countries', array(
    'group'                         => 'General',
    'type'                          => 'varchar',
    'input'                         => 'multiselect',
    'label'                         => 'Restrict shipping to',
    'backend'                       => 'eav/entity_attribute_backend_array',
    'visible'                       => 1,
    'required'                      => 0,
    'user_defined'                  => 1,
    'configurable'                  => 1,
    'global'                        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source'                        => 'customer/entity_address_attribute_source_country'
));

$installer->endSetup();
Was it helpful?

Solution

If I were you I'd not set the options manually, but use an existing source. You can replace the 'option' by 'source'. That's how it's done for customers as well. You can take a look at "app/code/core/Mage/Customer/Model/Entity/Address/Attribute/Source/Country.php".

You can just use customer/entity_address_attribute_source_country as the source or create your own source model in your own extension, which is the cleanest solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top