Monday 7 March 2016

How to add auto generate product code in magento


How to add auto generate product code in magento

Kindly follow below steps to create auto product code in magento.


First we need to add new column in `catalog_product_entity` table. To add new column kindly add below code in any module setup file.

For example :

I am using 'Banner' module in mysite so i put below code in app\code\local\Setblue\Banner\sql\banner_setup\mysql4-install-0.1.0.php

before you put below code you must take backup of that code of that file or just comment that code. then put below code in that file


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

$installer->startSetup();

$installer->getConnection()->addColumn(
    $installer->getTable('catalog/product'),
    'auto_code',
    array(
        'type'    => Varien_Db_Ddl_Table::TYPE_TEXT,
        'length'  => 255,
        'comment' => 'Auto Generate Code'
    )
);

$installer->addAttribute(
    'catalog_product',
    'auto_code',
    array(
        'label' => 'Auto Generate Code',
        'type'  => 'static'
    )
);

$installer->endSetup();

Now Remove Banner(Your module) module entry from 'core_resource' table. So we can run banner module setup file.

After that you must run that module (Ex. Banner) from admin panel.

Now 'auto_code' field is added into table `catalog_product_entity`.

Now we need to execute SQL Trigger when new product will inserted into database from manually (admin panel) or importing product sheet.

Follow the following steps for the same.

Add trigger code on 'catalog_product_entity_decimal' table.

1. Go to phpmyadmin and Select your database and open table 'catalog_product_entity_decimal.

2. Now click on 'Triggers' Option.and click on 'Add trigger'.

3. Trigger name : Enter trigger name (any) like 'Auto Code trigger'

4. Table : Select table `catalog_product_entity_decimal` from trigger popup. it means on which table you want fire trigger.

5. Time :  After (it means it will fire after product added into `catalog_product_entity_decimal` table)

6. Event : Insert (it means it will fire after product inserted into `catalog_product_entity_decimal` table)

7. Definition : we need to add below code.

   UPDATE catalog_product_entity SET auto_code = CONCAT('FP-',new.entity_id) where entity_id = new.entity_id

  in this 'FP-' this is static prefix before auto increment number.and entity_id is product number(primary key)

8. Definer : optional


Now Insert a product and check the entry.

That's it. Enjoy Chandresh Rana's coding... 

1 comment:

Anonymous said...

Hi, I do believe this is a great web site. I stumbledupon it ;) I am going to return yet again since
I saved as a favorite it. Money and freedom is the greatest way to change, may you be rich and
continue to help other people.