Magento的:增加新的属性到所有产品 [英] Magento: add new attribute to all products

查看:119
本文介绍了Magento的:增加新的属性到所有产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个新的属性添加到所有产品。我有一个安装脚本低谷做到了

  $安装= $这一点;
$ installer-> startSetup();$这个 - >的addAttribute('catalog_product','test2的,阵列(
    标签= GT; TEST2,
    '型'=> '为varchar',
    可见的=>真正,
    必要= GT;假,
    必要= GT; 0
));

但我怎么能由

值添加到这个属性

  $ entityTypeId = $ installer-> getEntityTypeId('catalog_product');
$ attributeSetId = $ installer-> getDefaultAttributeSetId($ entityTypeId);
$ attributeGroupId = $ installer-> getDefaultAttributeGroupId($ entityTypeId,$ attributeSetId);
$ installer-> addAttributeGroup($ entityTypeId,默认,测试2,0);
$ installer-> endSetup();


解决方案

这是样品code,我曾用于创建自己的自定义产品属性之一: -

  $安装= $这一点;
/ * @var $安装Mage_Core_Model_Resource_Setup * /$ installer-> startSetup();$ ATTR code ='测试2';
$ attrGroupName ='测试组';
$ attrLabel ='测试2';
$ attrNote ='测试注意事项;$ objCatalogEavSetup =法师:: getResourceModel('目录/ eav_mysql4_setup','core_setup');
$ attrIdTest = $ objCatalogEavSetup-> getAttributeId(Mage_Catalog_Model_Product ::实体,$ ATTR code);如果($ attrIdTest === FALSE){
    $ objCatalogEavSetup->的addAttribute(Mage_Catalog_Model_Product ::实体,$ ATTR code,阵列(
        '组'=> $ attrGroupName,
        SORT_ORDER'=> 7,
        '型'=> '为varchar',
        '后台'= GT; '',
        '前端'=> '',
        标签= GT; $ attrLabel,
        '注意'=> $ attrNote,
        输入=> '文本',
        类= GT; '',
        '源'=> '',
        全球性= GT; Mage_Catalog_Model_Resource_Eav_Attribute :: SCOPE_GLOBAL,
        可见的=>真正,
        必要= GT;假,
        USER_DEFINED'=>真正,
        默认=> 0,
        visible_on_front'=>假,
        '独特'=>假,
        is_configurable'=>假,
        used_for_promo_rules'=>真正
    ));
}$ installer-> endSetup();

这是用于这两篇文章的参考文献: -

此外,你会发现,我已经使用了数组键提属性组名称,在这个新的自定义属性将驻留。具有讽刺意味的​​是,提此键,上面code样品中,会自动创建此属性在每一个属性集在这个Magento的发现。

所以,你不需要调用任何方法(如 addAttributeToSet()),该属性添加到所有属性设置。

希望它帮助。

I want to add a new attribute to all products. I have done it with a install script trough

$installer = $this;
$installer->startSetup();

$this->addAttribute('catalog_product','test2',array(
    'label'     => 'test2',
    'type'      => 'varchar',
    'visible'   => true,
    'required'  => false,
    'required'  => 0
));

But how can I add values to this attribute by

$entityTypeId     = $installer->getEntityTypeId('catalog_product');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttributeGroup($entityTypeId, 'Default', 'test2', 0);
$installer->endSetup();

解决方案

This is one of the sample code which I had used to create my own custom Product Attribute:-

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$attrCode = 'test2';
$attrGroupName = 'Test Group';
$attrLabel = 'Test 2';
$attrNote = 'Test Note';

$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');
$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $attrCode);

if ($attrIdTest === false) {
    $objCatalogEavSetup->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, array(
        'group' => $attrGroupName,
        'sort_order' => 7,
        'type' => 'varchar',
        'backend' => '',
        'frontend' => '',
        'label' => $attrLabel,
        'note' => $attrNote,
        'input' => 'text',
        'class' => '',
        'source' => '',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => '0',
        'visible_on_front' => false,
        'unique' => false,
        'is_configurable' => false,
        'used_for_promo_rules' => true
    ));
}

$installer->endSetup();

This is used with the references of these two articles:-

Also, you will find that I have used the array key "group" to mention the Attribute Group Name, where this new custom Attribute will reside. The irony is that mentioning of this key, in the above code sample, automatically creates this Attribute in every Attribute Set found in this Magento.

So you do not need to call any method (like "addAttributeToSet()") to add this Attribute to all Attribute Sets.

Hope it helps.

这篇关于Magento的:增加新的属性到所有产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆