Search code examples
phpmagentozend-frameworkmigrationmagento-1.9

how to add an attribute programmatically to product in magento1.9?


I'm beginner in Magento, and I used Magento1.9 CE, I want to add an attribute, programmatically, in catalog/product. I mean, that I want to see it in orange box that I highlighted on

This Image

I change version in magento/app/code/core/Mage/Catalog/etc/config.xml file

`<modules>
    <Mage_Catalog>
        <version>1.6.0.0.19.1.15</version>
    </Mage_Catalog>
 </modules>`

and I add this file /magento/app/code/core/Mage/Catalog/sql/catalog_setup/mysql4-data-upgrade-1.6.0.0.19.1.15.php

$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'promotion', array(
    'group'             => 'promotion',
    'type'              => 'text',
    'backend'           => 
    'catalog/product_attribute_backend_promotion',
    'frontend'          => '',
    'label'             => 'promotion',
    'input'             => 'text',
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Eav_Model_Entity_Setup::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => '',
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'apply_to'          => 'simple,virtual',
    'is_configurable'   => false
));

when I refreshed add product page, In database core_resource table, catalog_setup version changed to 1.6.0.0.19.1.15 but nothing happend to eav_attribute

what should I do to add 'promotion' in eav_attribute table?


Solution

  • Step1: first create a php file.

    Step2: write below code in file.

    <?php 
    require_once('app/Mage.php');
    Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)); 
    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    $installer->addAttribute('catalog_product', 'custom_att', array(
                'group'           => 'General',
                'label'           => 'Custom att',
                'input'           => 'text',
                'type'            => 'varchar',
                'required'        => 0,
                'visible_on_front'=> 1,
                'filterable'      => 0,
                'searchable'      => 0,
                'comparable'      => 0,
                'user_defined'    => 1,
                'is_configurable' => 0,
                'global'          => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                'note'            => '',
    ));
    $installer->endSetup();
    ?>
    

    Step3: put this file on root and run this file by url. Then product attribute are created.