Create product attribute programmatically in Magento

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:

Create file /app/code/local/YourVendor/YourVendor/Model/Mysql4/Setup.php

with content:

Create file /app/code/local/YourVendor/YourModule/sql/yourmodule_setup/mysql4-install-1.0.0.php

with content:

In this file:
“1.0.0” is your module version which is declared in /etc/config.xml file.

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:

 

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.

Create product attribute programmatically in MagentoThat’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.

Create custom product type in Magento

Create custom product type in Magento – As you know, in Magento, there are 6 product types:
– Simple products
– Configurable products
– Grouped products
– Bundle products
– Virtual products
– Downloadable products

In this tutorial, we will show you how to create a custom product type in Magento. If you don’t have your own module, please create a simple one following this post.

To create a new product type, we need to do:
– Edit your config.xml file to declare your new product type
– Create a model for your product type
– Create a model for your product type price

Let’s do it step by step.

1. Edit your config.xml file.

Open file /app/code/local/YourVendor/YourModule/etc/config.xml and add content:

– ‘special‘ is your product type code.
‘yourmodule/catalog/product/type/special‘ is your product type model.
– ‘yourmodule/catalog/product/type/special‘ is your product type price model.

2. Create product type model.

Create new file /app/code/local/YourVendor/YourModule/Model/Catalog/Product/Type/Special.php with content:

3. Create product type price model

Create new file /app/code/local/YourVendor/YourModule/Model/Catalog/Product/Type/Sepecial/Price.php with content:

In those 2 classes, you can override some functions of Magento core product type. You can find parent classes at:
/app/code/core/Mage/Catalog/Model/Product/Type/Abstract.php
/app/code/core/Mage/Catalog/Model/Product/Type/Price.php
At last, refresh all cache types and try to create a new product to check if Magento recognized your new product type.

create custom product type in Magento
create custom product type in Magento

 

If you have any question, leave a comment or contact us.