Programmatically Assign Product Attribute to Attribute Set in Magento 2

In Magento, Assigning a single attribute to a single attribute set is a quick process and we can be done on the admin side very easily. But assigning the bulk of attributes to all the attribute sets (i.e, attribute sets with more then 25) is not a quick process and which require lots of effort. But we can make the process very simple by using a custom script. Here, I am going to show you how to assign the product attributes to all the attribute sets by using a custom script.

Assign Specific Attribute to Any Single Attribute Set:

The following code helps to assign a specific product attribute to any specific attribute set by using attribute code and attribute set name,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

/* Assign Specific Product Attribute to Any Single Attribute Set */
$attributeCode = 'size';
$attributeGroup = 'Product Details';
$attributeSet = 'Bag';
$eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
$config = $objectManager->get(\Magento\Catalog\Model\Config::class);
$attributeManagement = $objectManager->get(\Magento\Eav\Api\AttributeManagementInterface::class);

$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSet = $eavSetup->getAttributeSet($entityTypeId, $attributeSet);
if(isset($attributeSet['attribute_set_id'])) {
     $group_id = $config->getAttributeGroupId($attributeSet['attribute_set_id'], $attributeGroup);
     $attributeManagement->assign(
          'catalog_product',
          $attributeSet['attribute_set_id'],
          $group_id,
          $attributeCode,
          100
     );
}

Assign Specific Attribute to All Attribute Set:

The following code helps to assign a product attributes to all attribute sets,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

/* Assign Specific Product Attribute to All the Attribute Sets */
$attributeCode = 'size';
$attributeGroup = 'Product Details';

$eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
$config = $objectManager->get(\Magento\Catalog\Model\Config::class);
$attributeManagement = $objectManager->get(\Magento\Eav\Api\AttributeManagementInterface::class);

$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);
foreach ($attributeSetIds as $attributeSetId) {
     if ($attributeSetId) {
          $group_id = $config->getAttributeGroupId($attributeSetId, $attributeGroup);
          $attributeManagement->assign(
               'catalog_product',
               $attributeSetId,
               $group_id,
               $attributeCode,
               100
          );
     }
}

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *