Sep 4, 2014
kalpesh

Magento add/remove product attribute programatically

Magento add product attribute using sql setup file in your module. Also assign your custom attribute to attribute set Default and group General programatically.

Below code will add your new attribute in Manage Products edit screen at the end of General tab with dropdown values Yes/No. It will not display in frontend website but you can change visibible on front to 1 if you wish. Note that it will assign to Default attribute set only but you can change it to whatever as per your requirement.

$model = Mage::getResourceModel('catalog/setup','catalog_setup');

$data=array(
'type'=>'int',
'input'=>'boolean', //for Yes/No dropdown
'sort_order'=>50,
'label'=>'CUSTOM ATTRIBUTE CODE LABEL',
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required'=>'0',
'comparable'=>'0',
'searchable'=>'0',
'is_configurable'=>'1',
'user_defined'=>'1',
'visible_on_front' => 0, //want to show on frontend?
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'required'=> 0,
'unique'=>false,
'apply_to' => 'configurable', //simple,configurable,bundled,grouped,virtual,downloadable
'is_configurable' => false
);

$model->addAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE',$data);

$model->addAttributeToSet(
    'catalog_product', 'Default', 'General', 'CUSTOM_ATTRIBUTE_CODE'
); //Default = attribute set, General = attribute group

To remove the product attribute using the setup file, use below code instead:

$model = Mage::getResourceModel('catalog/setup','catalog_setup');
$model->removeAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE');

6 Comments

  • -Thanks Bro. it’s really very helpful.

  • Path to add the above code? How should i want to name this??

  • Hi Sir, I have been trying to find a script to add products with custom attributes programmatically. Could you please help me to do the same.

  • How to add timstamp attribute in catalog prodcut

  • I think that there is another standard way is to use Setup Model in Magento:
    Example: (for eav model, can also used for normal model)
    $installer = Mage::getModel(‘eav/resource_setup’);
    $installer->removeAttribute($modelAlias, $attributeCode);

  • Great Work! Attribute are used for multiple purposes in Magento. There’s a very easy to follow guide for creating an attribute and attribute set programmatically in Magento on magenticians. I found it very easy to understand, I hope it helps others too. thanks

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions