How to Programmatically Assign Attribute to All Attribute Sets in Magento 2

Written by Jigar Patel

Jun 17, 2023

How to Programmatically Assign Attribute to All Attribute Sets in Magento 2

If you have a product attribute and want to assign it to a single attribute set, it’s easy to do via Magento 2 Admin Panel.

These attributes can be assigned to different attribute sets to group related attributes together. Here are the steps to attribute to all attribute sets in Magento 2:

  1. Log in to your Magento 2 admin panel.
  2. Go to “Stores” in the main menu and click on “Attribute Set” under the “Attributes” section.
  3. On the Attribute Set page, you will see a list of existing attribute sets. Click on the attribute set you want to add attributes to, or click on “Default” to modify the default attribute set.
  4. On the Attribute Set edit page, you will see two columns: “Unassigned Attributes” and “Groups.”
  5. In the “Unassigned Attributes” column, you will find a list of available attributes that are not assigned to any group within the attribute set.
  6. Drag and drop the desired attributes from the “Unassigned Attributes” column to the appropriate group under the “Groups” column. If you want to attribute the attribute to all attribute sets, you need to repeat this step for each attribute set individually.
  7. Once you have assigned the attributes to the desired groups, click on the “Save Attribute Set” button to save your changes.

By following these steps, you can attribute attributes to all attribute sets in Magento 2.

Assign Attribute to All Attribute Sets using CLI command

To assign an attribute to all attribute sets in Magento 2, you can use the Magento command-line interface (CLI) or write a custom script. Here’s an example of how to achieve this using the CLI:

  1. Open a terminal or command prompt and navigate to your Magento 2 installation directory.
  2. Run the following command to get a list of all attribute sets and their IDs:
    bin/magento catalog:product:attribute:set:list

    This command will display a table with attribute set names and their corresponding IDs. Note down the ID of the attribute set(s) you want to assign the attribute to.

  3. Next, use the following command to assign the attribute to the desired attribute set(s):
    bin/magento catalog:product:attribute:set:assign <attribute_id> <attribute_set_id1> <attribute_set_id2> ...

    Replace <attribute_id> with the ID of the attribute you want to assign, and <attribute_set_id1>, <attribute_set_id2>, etc., with the IDs of the attribute sets you want to assign the attribute to. You can specify multiple attribute set IDs separated by spaces.

    For example, if the attribute ID is “123” and you want to assign it to attribute set IDs “1”, “2”, and “3”, the command would be:

    bin/magento catalog:product:attribute:set:assign 123 1 2 3
  4. After running the command, Magento will assign the attribute to the specified attribute sets. You can verify the assignment by checking the attribute sets in the Magento admin panel.

Assign Attribute to All Attribute Sets using custom script

1. Create a PHP file “attributeAssign.php” in your Magento 2 root directory.

2. Add the below code to the file.

3. Change the value for $ATTRIBUTE_CODE and $ATTRIBUTE_GROUP And save the fiel.

4. Run cli command. (PHP -f attributeAssign.php)

error_reporting(E_ALL);
ini_set('display_errors', 1);

$ATTRIBUTE_CODE = 'my_attribute';
$ATTRIBUTE_GROUP = 'General';

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

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

/* Attribute assign logic */
$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, $ATTRIBUTE_GROUP);
        $attributeManagement->assign(
            'catalog_product',
            $attributeSetId,
            $group_id,
            $ATTRIBUTE_CODE,
            999
       );
    }
}

We can assign an attribute to attribute set programmatically using the interface, Magento\Catalog\Api\ProductAttributeManagementInterface

Base Definition of the method to assign attribute,

public function assign($attributeSetId, $attributeGroupId, $attributeCode, $sortOrder);

$attributeSetId indicate Attribute set id.
$attributeGroupId indicate Attribute group id.
$attributeCode indicates the attribute code you want to assign to a specific attribute group.
$sortOrder indicates the sort order of the attribute in the group from the list of the attribute of the same attribute group.

I Hope, This instruction will be helpful for you.
Happy Coding with magento2!! 🙂
If you have any difficulties regarding this blog, do consider posting them in the Comments section below!

I’m here to help.
Thank you!

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP