Programmatically Add/Remove Attribute Group in Magento 2

As we all know that, we can add/remove attribute set, attribute group and product attribute on Magento admin side very easily. But sometimes like import bulk of data from another environment to Magento, creating the attribute groups and product attributes one by one on the admin side is not an easy work. But we can make the process simple by creating a custom script. In my previous blogs, I have explained to create “Product Attribute Set” & “Product Attribute” by using a custom script. Here I am going to show you, how to add/remove product attribute group in Magento 2 programmatically.

To work with the attribute group in Magento 2, we might need to inject the object of \Magento\Eav\Setup\EavSetup class in our script. In the following article, I am going to show the example scripts to

  • Add new attribute group in Magento 2
  • Get attribute group id by using attribute group name
  • Remove attribute group by using attribute group name or id

Add New Attribute Group:

Here is the example script to create new product attribute group to any specific attribute set in Magento 2,

/* Add Attribute Group to Any Specific Attribute Set */
$attributeGroupCode = 'Product Information';
$attributeSet = 'Bag';

$eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSet = $eavSetup->getAttributeSet($entityTypeId, $attributeSet);

if(isset($attributeSet['attribute_set_id'])) {
    $eavSetup->addAttributeGroup(
        $entityTypeId,
        $attributeSet['attribute_set_id'],
        $attributeGroupCode,
        100
    );
}

Get Attribute Group Id:

After creating the attribute group in Magento 2, we can get the attribute group id by using the group name as follows,

/* Retrieve Attribute Group Id by Group Name or Id */
$attributeGroupCode = 'Product Information';
$attributeSet = 'Bag';

$eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSet = $eavSetup->getAttributeSet($entityTypeId, $attributeSet);

if(isset($attributeSet['attribute_set_id'])) {
	$groupId = $eavSetup->getAttributeGroupId(
		$entityTypeId, 
		$attributeSet['attribute_set_id'], 
		$attributeGroupCode
	);
        print_r($groupId);
}

Remove Attribute Group:

If we don’t want any product attribute group, we can remove it by using the group name or group id as follows,

/* Remove Attribute Group by Group Name or Id */
$attributeGroupCode = 'Product Information';
$attributeSet = 'Bag';

$eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSet = $eavSetup->getAttributeSet($entityTypeId, $attributeSet);

if(isset($attributeSet['attribute_set_id'])) {
	$eavSetup->removeAttributeGroup(
		$entityTypeId, 
		$attributeSet['attribute_set_id'], 
		$attributeGroupCode
	);
}

Hope this helps.

Leave a Reply

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