19 Jan 2014

Magento: Creating custom attribute using sql setup in custom module

/* Magento: Creating custom attribute using sql setup in custom module */
Here I am going show how to create a simple product attribute programatically.I have created a simple script which is creating the product attribute when I do install my module or upgrade my module.So please look at the below code which I have created.


Please check the sample example on below.
$installer = $this;//instanciate the installer
$installer->startSetup();
$installer->addAttribute('attributeType(productattribute)', 'attributename', array(
    'group'              => 'attributeGroup',
    'type'               => 'data type',
    'backend'            => '',
    'frontend'           => '',
    'label'              => 'Label Of attribute',
    'input'              => 'attribute type',
    'class'              => '',
    'source'             => 'source of attribute value',
    'global'             => 'scope of attribute(Global/Website)',
    'visible'            => true,
    'required'           => false,
    'user_defined'       => false,
    'default'            => 'No',
    'searchable'         => false,
    'filterable'         => false,
    'comparable'         => false,
    'visible_on_front'   => false,
    'unique'             => false,
    'apply_to'           => '',
    'is_configurable'    => false      
));
$installer->endSetup();


You can see the main real example on below.

$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'email_lowest_price', array(
    'group'              => 'General',
    'type'               => 'int',
    'backend'            => '',
    'frontend'           => '',
    'label'              => 'Email lowest price',
    'input'              => 'select',
    'class'              => '',
    'source'             => 'eav/entity_attribute_source_boolean',
    'global'             => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'            => true,
    'required'           => false,
    'user_defined'       => false,
    'default'            => 'No',
    'searchable'         => false,
    'filterable'         => false,
    'comparable'         => false,
    'visible_on_front'   => false,
    'unique'             => false,
    'apply_to'           => '',
    'is_configurable'    => false      
));
$installer->endSetup();



In the above code My attribute label is Email lowest price,attribute code name is email_lowest_price.
This can be placed at app\code\local\JL\Emaillowest\data\jl_emaillowest_setup\data-install-0.1.0.php .
This can be placed at app\code\local\MyNamesapce\MyModule\data\module_setup\data-install-0.1.0.php .

As Like to place all file related to data in data folder so I placed in data folder but you can place this setup file at sql folder .

No comments:

Post a Comment