当我有多个属性集时(Attribute Set),新建了一个产品字段我想单独显示在某一个属性集,该怎么办?
如果我想显示在所有属性集,该怎么办?
如何通过编程的手段将属性加入属性集?
避免这种自动添加所有集合行为的 方法是设置 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'
);