如何以编程方式向 magento1.9 中的产品添加属性?

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

我是Magento的初学者,我用的是Magento1.9 CE, 我想以编程方式在 catalog/product 中添加一个属性。 我的意思是,我想在

上突出显示的橙色框中看到它

This Image

我在 magento/app/code/core/Mage/Catalog/etc/config.xml 文件中更改版本

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

然后我添加这个文件 /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
));

当我刷新添加产品页面时,数据库 core_resource table、catalog_setup 版本更改为 1.6.0.0.19.1.15 但 eav_attribute 没有任何反应

在eav_attributetable中添加'promotion'怎么办?

您不应更改核心模块的任何内容。 首先,您需要在 magento 中创建一个本地模块,然后才能添加产品属性 programatically.That 是添加属性的正确方法。

此链接可以帮助您创建产品属性

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

如果您不知道在 magento 中创建新模块意味着参考这个 url

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

如果您需要进一步的帮助,请问我。

第一步:首先创建一个php文件。

第二步:在文件中写入以下代码。

<?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();
?>

第 3 步:将此文件放在根目录下,然后 运行 此文件 url。然后创建产品属性。