【问题标题】:Magento 2: How to add an product attribute to multiple attribute sets programmatically?Magento 2:如何以编程方式将产品属性添加到多个属性集?
【发布时间】:2018-09-11 02:08:57
【问题描述】:

如何以编程方式将产品属性添加到多个属性集?

Magento 1 已回答此问题。它如何与 Magento 2 一起工作?

【问题讨论】:

  • 你找到答案了吗?

标签: attributes magento2


【解决方案1】:

当你添加一个属性时,它会被添加为基于模式 (\ 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'
        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多