Adding Multi-select Attributes using an Installer in Magento

Advertisements

I had to do a modification for a Magento install that required the use of adding a multi-select attribute. Most other people would just add the attribute by hand and forget about it, but I’m one for making the upgrade process easy. However, the Magento developers don’t seem to want you to know about it (or anything else for that matter, given the lack of documentation!!). Anyway, I eventually stumbled across a small snippet of code, and after a bit of tweaking to figure out where all the parameters went, the installer worked. Easy upgrading 🙂

// File: app/code/local/MyCompany/Catalog/sql/mysql4-install-0.1.php
// OR
// File: app/code/local/MyCompany/Catalog/sql/mysql4-upgrade-0.1-0.2.php

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

$installer->addAttribute('catalog_product', 'attr_id',array(
			 'label'             => 'Frontend Name',
			 'type'              => 'varchar',
			 'input'             => 'multiselect',
			 'backend'           => 'eav/entity_attribute_backend_array',
			 'frontend'          => '',
			 'source'            => '',
			 'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
			 'visible'           => true,
			 'required'          => false,
			 'user_defined'      => false,
			 'searchable'        => false,
			 'filterable'        => false,
			 'comparable'        => false,
			 'option'            => array (
			 	'value' => array('optionone' => array('First Option'),
			 			 'optiontwo' => array('Second Option'),
			 			 'optionthree' => array('Third Option'),
			 			 )
			 			),
			 'visible_on_front'  => false,
			 'visible_in_advanced_search' => false,
			 'unique'            => false
));

$installer->endSetup();

Of course, ‘addAttribute’ isn’t a method of $installer (or $this if you’re running a standard Mysql4 setup). So you have to extend a different setup with addAttribute defined. I found this one works well:

// File: app/code/local/MyCompany/Catalog/Model/Mysql4/Setup.php

class MyCompany_Catalog_Model_Mysql4_Setup extends Mage_Sales_Model_Mysql4_Setup {}

One thing to note with this however, is that the attribute will end up as a System attribute, and applied to every attribute set on the system. I’m currently working on a way around this, and will edit this post when I have something more to share. 🙂

Join the Conversation

6 Comments

  1. Thank you so much for this VERY HELPFUL post!

    I still have one question, how do you set the default option or the option order?

    Thanks Again!

  2. Hi there,

    This code currently only works with Simple Products – that don’t have any options. This example is merely a point to get other developers started, or for users to add a feature to their Magento installation without them having to go and do PHP code. There’s plenty of room for advancement in this to get it working for Configurable Products. in that you can add extra parameters to the query string to pass what you need through to the code and accommodate for that in the controller when looping through the list of items to add.

    You’ve obviously got to get something on the front-end where the multi-add javascript sits and also to allow the user to either select default option(s), or selects options when selecting products to add (e.g; when a user checks a checkbox in order to multi-add that product to the basket from the listing page, a small lightbox/jquery-ui dialog box could pop up and prompt the user for, say, quantity, size, color, whichever options are required). A bit of clever coding on this front could yield a very powerful feature. You just have to get the list of attributes and relevant options to the listing page (a trivial task).

    Obviously, if you need help, give me a shout on my contact form and we can work out some sort of arrangement to get your store doing exactly what you need it to do.

    Dan

  3. Thanks for the codes!

    I think, changing “user_defined” to “true” would make the attribute non-system.

    Question:

    What if I have multiple multi-select attributes each with its own options source to install?

    Thanks.

  4. You should be able to just run the addAttribute() function as many times as you need, once for each attribute you need to add. You’d need to change (or in this case, provide) the “source” value to be the key of your options source model (for example; customer/customer_attribute_source_store). So I’d do this;

    $installer = $this;
    $installer->startSetup();
    $installer->addAttribute('entity_type', 'attr_code1', array([values]));
    $installer->addAttribute('entity_type', 'attr_code2', array([values]));
    ..
    $installer->addAttribute('entity_type', 'attr_codeN', array([values]));
    $installer->endSetup();
    
  5. As for being applied to all attribute sets, try setting a ‘group’ field to the specific attribute set you want the attribute defined under. I know that if you say ‘group’ => ‘My_group’ it will place the attribute under this group, and create My_group if it doesn’t exist and place it under that.

Leave a comment

Your email address will not be published. Required fields are marked *