Magento:为所有产品添加新属性 [英] Magento: add new attribute to all products

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

问题描述

我想为所有产品添加一个新属性.我已经通过安装脚本完成了

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

但是如何通过

$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:-

此外,您会发现我使用了数组键group"来提及属性组名称,这个新的自定义属性将驻留在该名称中.具有讽刺意味的是,在上面的代码示例中提到这个键,会自动在这个 Magento 中找到的每个 Attribute Set 中创建这个 Attribute.

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.

因此您不需要调用任何方法(如addAttributeToSet()")来将此属性添加到所有属性集.

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

希望有帮助.

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

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