【发布时间】:2018-09-11 02:08:57
【问题描述】:
如何以编程方式将产品属性添加到多个属性集?
Magento 1 已回答此问题。它如何与 Magento 2 一起工作?
【问题讨论】:
-
你找到答案了吗?
标签: attributes magento2
如何以编程方式将产品属性添加到多个属性集?
Magento 1 已回答此问题。它如何与 Magento 2 一起工作?
【问题讨论】:
标签: attributes magento2
当你添加一个属性时,它会被添加为基于模式 (\ Magento \ Catalog \ Model \ Product ::ENTITY) 的集合。
基于默认创建的所有集合都将具有此属性,因为它们都有 entity_type/code = 4 (catalog_product)
为了避免以后不得不删除一些您不想要的集合的麻烦,请参见下文。
避免这种自动添加所有集合行为的唯一方法是设置 user_defined: true
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'attribute_name',
[
'type' => 'varchar',
'label' => 'Attribute Label',
'input' => 'text',
'required' => false,
'sort_order' => 5,
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'used_in_product_listing' => true,
'visible_on_front' => true,
// 'attribute_set' => 'AttributeSet Name', // Don't set this
'user_defined' => true,
// 'group' => 'Group Name', // Don't set this
]
);
这样属性将被创建并且不会与任何集合链接。 稍后要将属性链接到特定集,请参见下文。
$eavSetup->addAttributeToSet(
Product::ENTITY,
'AttributeSet Name',
'GroupName of AttributeSet', // Ex: General
'attribute_name'
);
$eavSetup->addAttributeToSet(
Product::ENTITY,
'Another AttributeSet Name',
'GroupName of Another AttributeSet', // Ex: "Existing group"
'attribute_name'
);
【讨论】: