Question

While creating set of product attributes via program, it gets assigned to existing Product Sets as well as to newly created Product Sets. I don't want to assign those created attribute to any Attribute Set.

Can anyone help me to solve this issue.

Code Used:

       $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'attribute_id',
        ['type' => 'varchar',
            'backend' => '',
            'frontend' => '',
            'label' => 'Attribute Name',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false

        ]
    );
Was it helpful?

Solution

Pass 'user_defined' => true to stop assigning attribute to any attribute set.

So your final code should look like

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'attribute_id',
    ['type' => 'varchar',
        'backend' => '',
        'frontend' => '',
        'label' => 'Attribute Name',
        'input' => 'text',
        'class' => '',
        'source' => '',
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => true,
        'unique' => false
    ]
);

Explain In Details:

I just debugged into EavSetup.php at vendor/magento/module-eav/Setup/EavSetup.php and found that Magento checks that if group key is not empty or user_defined key is empty then it assigns an attribute to all attribute set.

if (!empty($attr['group']) || empty($attr['user_defined'])) {
    ...
    ...
    foreach ($sets as $set) {
        if (!empty($attr['group'])) {
            $this->addAttributeGroup($entityTypeId, $set['attribute_set_id'], $attr['group']);
            $this->addAttributeToSet(
                $entityTypeId,
                $set['attribute_set_id'],
                $attr['group'],
                $code,
                $sortOrder
            );
        } else {
            $this->addAttributeToSet(
                $entityTypeId,
                $set['attribute_set_id'],
                $this->_generalGroupName,
                $code,
                $sortOrder
            );
        }
    }
}

We should set 'user_defined' => true` to custom attributes. Only Magento system attributes are set to false. System attributes cannot be deleted if you set false to user_defined, your attribute will work as a system attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top