Adding ‘catalog_product’ Type Attributes Programmatically in Mysql4 Install Files

By November 3, 2010Magento Code Snippets

Adding ‘catalog_product’ Type Attributes Programmatically in Mysql4 Install Files

Ron Peled 10:02 pm

If you want to write an extension that requires some additional product attributes, I recommend that you look in the Mage_Catalog core module. Specifically, you will want to check out the most recent mysql4-upgrade*.php files since they have the more up-to-date method of using the sql installer. On the right is a screenshot of the files to look for. How to set these files up and what to look for is a whole separate discussion.

Now, for the code snippet, notice how ‘new_product_size’ can be any attribute code you wish to add and in the array you have a variety of attributes options which eventually end up in a few tables in the DB and describe the attribute and its characteristics. Here is how you actually add an attribute:

$installer->addAttribute('catalog_product', 'new_product_size', array(
'backend' => '',
'frontend' => '',
'label' => 'Size',
'input' => 'select',
'class' => '',
'global' => true,
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'apply_to' => '',
'position' => 1,
'visible_on_front' => true,
'visible_in_advanced_search' => 1

I needed this a few times and spent too much time to find the answer, enjoy!

Ron Peled

About Ron Peled

Builder of things. Builder of teams. Passion: eCommerce & Marketplaces. Magento expert. CTO Mentor.