Create product attribute programmatically in Magento – In Magento, product is an EAV Model so to add a custom product attribute we need to create an EAV attribute. Before starting, make sure that you already prepared your own module. If you haven’t, you can create a simple module following this post.
In this tutorial, you need to to:
– edit your config.xml file
– create file /app/code/local/YourVendor/YourVendor/Model/Mysql4/Setup.php
– create file /app/code/local/YourVendor/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php
Edit your config.xml file, add this content:
1 2 3 4 5 6 7 8 9 10 11 12 |
<global> ... <resources> <yourmodule_setup> <setup> <module>YourVendor_YourModule</module> <class>YourVendor_YourModule_Model_Mysql4_Setup</class> </setup> </yourmodule_setup> </resources> ... </global> |
Create file /app/code/local/YourVendor/YourVendor/Model/Mysql4/Setup.php
with content:
1 2 3 4 5 6 |
<?php class YourVendor_YourModule_Model_Mysql4_Setup extends Mage_Core_Model_Resource_Setup { } ?> |
Create file /app/code/local/YourVendor/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php
with content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php $installer = $this; $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $installer->startSetup(); $setup->addAttribute('catalog_product', 'new_product_attribute', array( 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'New Product Attribute', 'group' => 'General', 'input' => 'text', 'class' => '', 'source' => '', 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'visible' => false, 'required' => false, 'user_defined' => true, 'default' => '', 'searchable' => true, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => array('simple', 'configurable', 'virtual', 'downloadable'), 'is_configurable' => false, 'is_used_for_promo_rules' => true, )); $installer->endSetup(); ?> |
In this file:
“1.0.0” is your module version which is declared in /etc/config.xml file.
1 2 3 4 5 6 7 8 |
<config> <modules> <YourVendor_YourModule> <version>1.0.0</version> </YourVendor_YourModule> </modules> ... </config> |
To create a new product attribute, we will use function addAttribute of model eav/entity_setup. You can find that function in file /app/code/core/Mage/Eav/Model/Entity/Setup.php and this is the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php /** * Add attribute to an entity type * * If attribute is system will add to all existing attribute sets * * @param string|integer $entityTypeId * @param string $code * @param array $attr * @return Mage_Eav_Model_Entity_Setup */ public function addAttribute($entityTypeId, $code, array $attr) { .... return $this; } ?> |
After editing the file, if you already installed your module on your server before, you need to re-install in to execute mysql4-install-1.0.0.php file. You also can upgrade your module to higher version as 1.1.0 by editing config.xml file and rename file ‘mysql-install-1.0.0.php‘ to ‘mysql4-upgrade-1.0.0-1.1.0.php‘
To re-install your module, go to your phpmyadmin, find table core_resouce and delete row which has code ‘yourmodule_setup‘.
Finally, flush cache storage.
When everything is done, you could see your new product attribute in ‘Backend->Catalog->Attribute->Manage Attributes‘ search ‘new_product_attribute‘ for Attribute Code.
That’s how to create product attribute programmatically in Magento. Hope it will be helpful.
If you have any question, leave a comment or contact us.