Question

In one of the modules we are developing we require to create a multi-select attribute with several options. It's highly important that these options are given a certain order by default.

This is the current code for adding the attribute:

$installer->addAttribute('catalog_product', 'tariffplan_sms_slider', array(
    'attribute_set'     => 'Randomattributeset',
    'group'             => 'Sample',
    'type'              => 'varchar',
    'default'           => 0,
    'required'          => false,
    'visible'           => true,
    'backend'           => 'eav/entity_attribute_backend_array',
    'frontend'          => '',
    'label'             => 'Random label',
    'note'              => '',
    'input'             => 'multiselect',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
    'option'            => array ('value' => 
                            array(
                                'one' => array("Low"),
                                'two' => array("Medium"),
                                'three' => array('High'),
                                'four' => array('Unlimited')
                            )
                        ),
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'default'           => 0,
    'visible_on_front'  => false,
    'used_in_product_listing'   => false,
    'unique'            => false
));

As you can see I'm giving an array of options for this attribute that will be created. On top of that I would like to set the order (see Position input fields in the backend) of each option.

I have tried the following below but it isn't working:

 'option'           => array (
                            'value' => array(
                                'one' => array("Don't care"),
                                'two' => array("Low"),
                                'three' => array('High'),
                                'four' => array('Unlimited')
                            ),
                            'order' => array(
                                'one' => 0,
                                'two' => 1,
                                'three' => 2,
                                'four' => 3
                            )
                        ),

Version: Magento ver. 1.7.0.1

Anyone got an idea?

Was it helpful?

Solution

Fixed it myself by looking at the addAttributeOption() method inside Mage_Eav_Model_Entity_Setup:

Result:

$installer->addAttribute('catalog_product', 'tariffplan_sms_slider', array(
    'attribute_set'     => 'Randomattributeset',
    'group'             => 'Sample',
    'type'              => 'varchar',
    'default'           => 0,
    'required'          => false,
    'visible'           => true,
    'backend'           => 'eav/entity_attribute_backend_array',
    'frontend'          => '',
    'label'             => 'Random label',
    'note'              => '',
    'input'             => 'multiselect',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
    'option'            => array (
                            'values' => array(
                                '1' => 'Low',
                                '2' => 'Medium',
                                '3' => 'High',
                                '4' => 'Unlimited'
                            )
                        ),
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'default'           => 0,
    'visible_on_front'  => false,
    'used_in_product_listing'   => false,
    'unique'            => false
));

For those that don't see it immediately you basically just needed to change the value you give to the "option" key from:

'option'            => array ('value' => 
                            array(
                                'one' => array("Low"),
                                'two' => array("Medium"),
                                'three' => array('High'),
                                'four' => array('Unlimited')
                            )
                        ),

TO (see how value changed ito into values):

'option'            => array (
                            'values' => array(
                                '1' => 'Low',
                                '2' => 'Medium',
                                '3' => 'High',
                                '4' => 'Unlimited'
                            )
                        ),
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top