Search code examples
phpmagentomagento-1.9

Extend category attribute set with select box (multiple choose)


Im trying to accomplish a select box with multiple option-choose in the backend of categories.

The script to create a select box is working so far, but only with a single-choose.

$installer = $this;
$installer->startSetup();

$attribute  = array(
        'group'                     => 'Examplegroup',
        'input'                     => 'select', // also tried multiselect
        'type'                      => 'varchar',
        'label'                     => 'Examplelabel',
        'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'                   => 1,
        'required'                  => 0,
        'visible_on_front'          => 0,
        'is_html_allowed_on_front'  => 0,
        'is_configurable'           => 0,
        'searchable'                => 0,
        'filterable'                => 1,
        'comparable'                => 0,
        'unique'                    => false,
        'user_defined'              => true,
        'default'                   => '',
        'is_user_defined'           => false,
        'used_in_product_listing'   => true,
        'option'                    => array('values' => array('option1', 'option2', 'option3', 'option4'))
);
$installer->addAttribute('catalog_category', 'attribute_name', $attribute);


$installer->endSetup();

How do I achieve this

multiselect

I assume it should work with the input-type of multiselect but it keeps a single-choose-option after upgrading.


Solution

  • For multiselect option, set input to multiselect and add backend model eav/entity_attribute_backend_array.

    $installer = $this;
    $installer->startSetup();
    
    $attribute  = array(
            'group'                     => 'Examplegroup',
            'input'                     => 'multiselect',
            'type'                      => 'varchar',
            'label'                     => 'Examplelabel',
            'backend'                   => 'eav/entity_attribute_backend_array',
            'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
            'visible'                   => 1,
            'required'                  => 0,
            'visible_on_front'          => 0,
            'is_html_allowed_on_front'  => 0,
            'is_configurable'           => 0,
            'searchable'                => 0,
            'filterable'                => 1,
            'comparable'                => 0,
            'unique'                    => false,
            'user_defined'              => true,
            'default'                   => '',
            'is_user_defined'           => false,
            'used_in_product_listing'   => true,
            'option'                    => array('values' => array('option1', 'option2', 'option3', 'option4'))
    );
    $installer->addAttribute('catalog_category', 'attribute_name', $attribute);
    
    
    $installer->endSetup();
    

    Run the following upgrade script to update existing attribute,

    $installer->startSetup();
    
    $installer->updateAttribute('catalog_category', 'attribute_name', 'frontend_input', 'multiselect');
    $installer->updateAttribute('catalog_category', 'attribute_name', 'backend_model', 'eav/entity_attribute_backend_array');
    
    $installer->endSetup();
    

    Check beforeSave function of Mage_Eav_Model_Entity_Attribute_Backend_Array class to get more idea of backend model.

    Hope it helps!