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.
By following these steps, you can attribute attributes to all attribute sets in Magento 2.
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:
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.
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
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!