문제

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();
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top