简体   繁体   中英

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?

You should not change anything from core modules. First you need to create a local module in magento then only you can able to add product attribute programatically.That is the proper way to add attribute.

This links may help you for create product attributes

http://inchoo.net/magento/programatically-create-attribute-in-magento-useful-for-the-on-the-fly-import-system/

https://magento.stackexchange.com/questions/162595/programmatically-add-custom-product-attribute-to-attribute-set

If you are not aware about create new module in magento means refer this url

http://inchoo.net/magento/programming-magento/magento-hello-world-module-extension/

If you need further assistance means ask me.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM