How to create new products attribute programmatically

Attributes can be also created manually in Catalog > Attributes.
However, if your custom module requires to use new attributes, then the best practice is to create them programmatically, so when you install the module, new attributes will be created automatically without any further actions.

<?php  
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute('catalog_product', 'magentoguides_attribute_key', array(
            'group'           => 'General',
            'label'           => 'Magento Guides front end label',
            'input'           => 'text',
            'type'            => 'varchar',
            'required'        => 0,
            'visible_on_front'=> 1,
            'filterable'      => 0,
            'searchable'      => 0,
            'comparable'      => 0,
            'user_defined'    => 1,
            'is_configurable' => 0,
            'global'          => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
            'note'            => '',
));
$installer->endSetup();

The new Attribute will be at the bottom of the product page by default. If you want to move it for example to prices tab. Then change ‘General’ to ‘Prices’. Or you can do it manually by navigating to¬†Catalog > Manage attribute sets

If instead you want to use a different type than ‘text’, change ‘type’ to for example ‘textarea’.


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.