Home » Magento9 December 2009

Magento: Adding attribute from MySql setup file

You can add attribute from Admin Panel -> Catalog -> Attributes -> Manage Attributes.

You can also add attributes from mysql setup file of your module. MySql setup file is present inside “YourModule/sql/yourmodule_setup” directory.

In the following example, the version of my module is 0.1.0.  I have added attribute for product. I have added a new Attribute Group called “Special Attributes” and then I have assigned the new attribute called “testing_attribute” into the “Special Attribute” group.

// file mysql4-install-0.1.0.php


$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
 * Adding Different Attributes

// adding attribute group
$setup->addAttributeGroup('catalog_product', 'Default', 'Special Attributes', 1000);

// the attribute added will be displayed under the group/tab Special Attributes in product edit page
$setup->addAttribute('catalog_product', 'testing_attribute', array(
	'group'     	=> 'Special Attributes',
	'input'         => 'text',
    'type'          => 'text',
    'label'         => 'Testing',
	'backend'       => '',
	'visible'       => 1,
	'required'		=> 0,
	'user_defined' => 1,
	'searchable' => 1,
	'filterable' => 0,
	'comparable'	=> 1,
	'visible_on_front' => 1,
	'visible_in_advanced_search'  => 0,
	'is_html_allowed_on_front' => 0,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,


Be careful on input and type. Input means the input type of the attribute. And type means the input type in database.

For textfield it will be:
'input' => 'text',
'type' => 'text',

For textarea it will be:
'input' => 'textarea',
'type' => 'text',

For date field it will be:
'input' => 'date',
'type' => 'datetime',

For select list it will be:
'input' => 'select',
'type' => 'text',

Here is the sample code to add datetime attribute. Please note that the type for date should be datetime and backend should be eav/entity_attribute_backend_datetime.

$setup->addAttribute('catalog_product', 'delivery_date', array(
	'group'     	=> 'Special Attributes',
	'input'         => 'date',
    'type'          => 'datetime',
    'label'         => 'Delivery',		  	
	'backend'		=> "eav/entity_attribute_backend_datetime",	
	'visible'       => 1,	
	'required'		=> 0,
	'user_defined' => 1,
	'searchable' => 1,
	'filterable' => 0,
	'comparable'	=> 0,
	'visible_on_front' => 1,
	'visible_in_advanced_search'  => 0,
	'is_html_allowed_on_front' => 0,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,   	

Adding custom options to dropdown attribute through setup file

You can also add custom options to dropdown attribute, like there is in manufacturer or color attribute.

Here is a sample code for this purpose:-

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('catalog_product', 'test_attribute', array(
			 'label'             => 'Test',
			 'type'              => 'varchar',
			 'input'             => 'select',
			 'backend'           => 'eav/entity_attribute_backend_array',
			 'frontend'          => '',	
			 'source'            => 'yourmodule/source_option',
			 'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
			 'visible'           => true,
			 'required'          => true,
			 'user_defined'      => true,
			 'searchable'        => false,
			 'filterable'        => false,
			 'comparable'        => false,
			 'option'            => array (
											'value' => array('optionone' => array('Sony'),
															 'optiontwo' => array('Samsung'),
															 'optionthree' => array('Apple'),												
			 'visible_on_front'  => false,
			 'visible_in_advanced_search' => false,
			 'unique'            => false


In the code above, we have added a dropdown attribute named ‘Test‘. Three options (Sony, Samsung, and Apple) are added to this attribute.

Note:- In the source value, you can see yourmodule/source_option. This means, you just need to create a file YourModule/Model/Source/Option.php with the following code:-


class YourNamespace_YourModule_Model_Source_Option extends Mage_Eav_Model_Entity_Attribute_Source_Table

Following are the allowed parameters for attributes


(see Mage_Eav_Model_Entity_Setup::addAttribute)

$data = array(
            'entity_type_id'            => $entityTypeId,
            'attribute_code'            => $code,
            'backend_model'             => $this->_getValue($attr, 'backend', ''),
            'backend_type'              => $this->_getValue($attr, 'type', 'varchar'),
            'backend_table'             => $this->_getValue($attr, 'table', ''),
            'frontend_model'            => $this->_getValue($attr, 'frontend', ''),
            'frontend_input'            => $this->_getValue($attr, 'input', 'text'),
            'frontend_input_renderer'   => $this->_getValue($attr, 'input_renderer', ''),
            'frontend_label'            => $this->_getValue($attr, 'label', ''),
            'frontend_class'            => $this->_getValue($attr, 'frontend_class', ''),
            'source_model'              => $this->_getValue($attr, 'source', ''),
            'is_global'                 => $this->_getValue($attr, 'global', 1),
            'is_visible'                => $this->_getValue($attr, 'visible', 1),
            'is_required'               => $this->_getValue($attr, 'required', 1),
            'is_user_defined'           => $this->_getValue($attr, 'user_defined', 0),
            'default_value'             => $this->_getValue($attr, 'default', ''),
            'is_searchable'             => $this->_getValue($attr, 'searchable', 0),
            'is_filterable'             => $this->_getValue($attr, 'filterable', 0),
            'is_comparable'             => $this->_getValue($attr, 'comparable', 0),
            'is_visible_on_front'       => $this->_getValue($attr, 'visible_on_front', 0),
            'is_html_allowed_on_front'  => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
                                        => $this->_getValue($attr, 'visible_in_advanced_search', 0),
            'is_used_for_price_rules'   => $this->_getValue($attr, 'used_for_price_rules', 1),
            'is_filterable_in_search'   => $this->_getValue($attr, 'filterable_in_search', 0),
            'used_in_product_listing'   => $this->_getValue($attr, 'used_in_product_listing', 0),
            'used_for_sort_by'          => $this->_getValue($attr, 'used_for_sort_by', 0),
            'is_unique'                 => $this->_getValue($attr, 'unique', 0),
            'apply_to'                  => $this->_getValue($attr, 'apply_to', ''),
            'is_configurable'           => $this->_getValue($attr, 'is_configurable', 1),
            'note'                      => $this->_getValue($attr, 'note', ''),
            'position'                  => $this->_getValue($attr, 'position', 0),

From Mukesh Chapagain's Blog, post Magento: Adding attribute from MySql setup file

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Chad

    This is great! Is there any way to add custom options of a drop down attribute through a setup file? Thanks!


    Thank you for this posting…….

  • http://blog.chapagain.com.np/ Mukesh

    Yes, we can add custom options to dropdown attribute through setup file. I have updated the article with your answer. Thanks.

  • Sushan Senanayake

    how can add customer type select box like this magento front end and backend?

  • Sergey Kolodyazhnyy

    > For select list it will be:
    > ‘input’ => ‘select’,
    > ‘type’ => ‘text’,
    You are wrong, type should be varchar or int. Text type you can use for texts like Description, Short Description etc.

  • PHPRelated

    Thank you. This was helpful.

  • Sandipgulati08

    how to add attribute in customer information in magento1.5.1

    plz tell

  • http://www.facebook.com/grigzie Andrew Griggs

    I’ve added an attribute into the sales_quote_item table using the mysql setup file (Thanks for showing how to do this!)

    I was wondering how you can edit this now? I can’t find an example of updateAttribute(). I want to change the mysql column so that it is a DATE column and not INT(10) which it is now.

    Thank again!

  • Caroline FRAMERY

    How do you do with a radio button ?

  • Amit Samtani

    HI Mukesh, Thanks for posting this it really has been very helpful.

    Quick question for you, I was trying to create an attribute that is filterable. But in Magento 1.5, the options that are in _prepareValues is as follows:

    protected function _prepareValues($attr)    {       
    $data = array();       
    $data = array(           
    ‘backend_model’ => $this->_getValue($attr, ‘backend’, ”),           
    ‘backend_type’ => $this->_getValue($attr, ‘type’, ‘varchar’),           
    ‘backend_table’ => $this->_getValue($attr, ‘table’, ”),           
    ‘frontend_model’ => $this->_getValue($attr, ‘frontend’, ”),           
    ‘frontend_input’ => $this->_getValue($attr, ‘input’, ‘text’),           
    ‘frontend_label’ => $this->_getValue($attr, ‘label’, ”),           
    ‘frontend_class’ => $this->_getValue($attr, ‘frontend_class’, ”),           
    ‘source_model’ => $this->_getValue($attr, ‘source’, ”),           
    ‘is_required’ => $this->_getValue($attr, ‘required’, 1),           
    ‘is_user_defined’ => $this->_getValue($attr, ‘user_defined’, 0),           
    ‘default_value’ => $this->_getValue($attr, ‘default’, ”),           
    ‘is_unique’ => $this->_getValue($attr, ‘unique’, 0),         
      ‘note’ => $this->_getValue($attr, ‘note’, ”),
    return $data;   

    Its missing many attributes that you have in yours, like:

    ‘is_searchable’ => $this->_getValue($attr, ‘searchable’, 0),
    ‘is_filterable’ => $this->_getValue($attr, ‘filterable’, 0)

    Were those removed? Were they removed for a reason? Or did you override the Setup class?

    Looking forward to your response and again, thanks for posting this,

  • Pingback: Attribute in eigener Extension per Setup hinzufügen | Miragento()

  • Pingback: Magento tips « Peter Jaap's Blog()

  • Test

    How order option with multiselect??

  • Pingback: Adding new attributes | Magento Media()

  • Pingback: Magento Adding new customer attributes | Magento Media()

  • Cannycookie

    Do you know if its possible to allow a file upload attribute?

  • Peter

    you are a hero for posting stuff like this!!! Fact!

  • Sean Villani

    if your don’t want to make your own source table file, or are lazy:

    ‘source’ => ‘eav/entity_attribute_source_table’,

    Great post!

  • Soufiane Marar

    how about adding attribute to an existing group like General ?