{"id":1768,"date":"2021-05-16T06:02:57","date_gmt":"2021-05-16T06:02:57","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/programmatically-assign-attribute-to-all-attribute-sets-in-magento-2\/"},"modified":"2025-07-16T17:51:21","modified_gmt":"2025-07-16T12:21:21","slug":"programmatically-assign-attribute-to-all-attribute-sets-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-assign-attribute-to-all-attribute-sets-in-magento-2\/","title":{"rendered":"How to Programmatically Assign Attribute to All Attribute Sets in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0provides a flexible functionality to set properties to each product in a catalog. You can configure product features such as color, size, width, height, etc using Magento product attributes.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Specific product attribute assigns to the&nbsp;<a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/stores-sales\/guide-overview\" target=\"_blank\" rel=\"noreferrer noopener\">attribute set<\/a>, which fully describes all product characteristics. The attribute set is used during every new product creation. We can assign an attribute to the attribute set at&nbsp;<strong>Magento 2 Admin Panel<\/strong>&nbsp;&gt;&nbsp;<strong>Stores<\/strong>&nbsp;&gt;&nbsp;<strong>Attributes<\/strong>&nbsp;&gt;&nbsp;<strong>Attribute Set<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, what if you want to assign an attribute to all attribute sets? Let\u2019s assume you have fifteen attribute sets, and you want to assign your attribute to all attribute sets! Won\u2019t it be tiresome and too much effort to assign attributes to all attribute sets from admin individually?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, you have a module that works only when the particular attribute is assigned to all required attribute sets. Do you prefer to assign it manually whenever that module is being installed? Obviously, no!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In such a scenario, use the below method to&nbsp;programmatically assign attribute to all attribute sets in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Programmatically Assign Attribute to All Attribute Sets in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. If you already created a product attribute then just assign it to all attribute sets.<br>Create a PHP file \u201c<strong>AssignAttributes.php<\/strong>\u201d in your Magento 2 root directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nerror_reporting(E_ALL);\nini_set('display_errors', 1);\n\n$ATTRIBUTE_CODE = 'attribute_text';\n$ATTRIBUTE_GROUP = 'General';\n\nuse Magento\\Framework\\App\\Bootstrap;\n\nrequire __DIR__ . '\/app\/bootstrap.php';\n\n$bootstrap = Bootstrap::create(BP, $_SERVER);\n\n$objectManager = $bootstrap->getObjectManager();\n$state = $objectManager->get(Magento\\Framework\\App\\State::class);\n$state->setAreaCode('adminhtml');\n\n\/* Attribute assign logic *\/\n$eavSetup = $objectManager->create(\\Magento\\Eav\\Setup\\EavSetup::class);\n$config = $objectManager->get(\\Magento\\Catalog\\Model\\Config::class);\n$attributeManagement = $objectManager->get(\\Magento\\Eav\\Api\\AttributeManagementInterface::class);\n\n$entityTypeId = $eavSetup->getEntityTypeId(\\Magento\\Catalog\\Model\\Product::ENTITY);\n$attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);\nforeach ($attributeSetIds as $attributeSetId) {\n    if ($attributeSetId) {\n        $group_id = $config->getAttributeGroupId($attributeSetId, $ATTRIBUTE_GROUP);\n        $attributeManagement->assign(\n            'catalog_product',\n            $attributeSetId,\n            $group_id,\n            $ATTRIBUTE_CODE,\n            999\n        );\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Change the value for&nbsp;<strong>$ATTRIBUTE_CODE<\/strong>&nbsp;and&nbsp;<strong>$ATTRIBUTE_GROUP<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. When you create a custom module and a custom product attribute then assign this attribute to all attribute sets.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Setup;\n\nuse Magento\\Eav\\Setup\\EavSetupFactory;\nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n\nclass InstallData implements InstallDataInterface\n{\n    protected $eavSetupFactory;\n\n    public function __construct(EavSetupFactory $eavSetupFactory)\n    {\n        $this->eavSetupFactory = $eavSetupFactory;\n    }\n\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);\n\n        $eavSetup->removeAttribute(\\Magento\\Catalog\\Model\\Product::ENTITY, 'custom_product_attribute');\n        $eavSetup->addAttribute(\n            \\Magento\\Catalog\\Model\\Product::ENTITY,\n            'custom_product_attribute',\n            [\n\n                'type' => 'varchar',\n                'backend' => '',\n                'frontend' => '',\n                'label' => 'Attribute Label',\n                'input' => 'text',\n                'class' => '',\n                'source' => '',\n                'global' => \\Magento\\Eav\\Model\\Entity\\Attribute\\ScopedAttributeInterface::SCOPE_STORE,\n                'group' => 'Attribute Group',\n                'visible' => false,\n                'required' => false,\n                'user_defined' => false,\n                'default' => '',\n                'searchable' => false,\n                'filterable' => false,\n                'comparable' => false,\n                'visible_on_front' => false,\n                'used_in_product_listing' => false,\n                'unique' => false,\n                'apply_to' => ''\n            ]\n        );\n\n\n        $ATTRIBUTE_GROUP = 'General'; \/\/ Attribute Group Name\n        $entityTypeId = $eavSetup->getEntityTypeId(\\Magento\\Catalog\\Model\\Product::ENTITY);\n        $allAttributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);\n        foreach ($allAttributeSetIds as $attributeSetId) {\n            $groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, $ATTRIBUTE_GROUP);\n            $eavSetup->addAttributeToGroup(\n                $entityTypeId,\n                $attributeSetId,\n                $groupId,\n                'custom_product_attribute',\n                null\n            );\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0provides a flexible functionality to set properties to each product in a catalog. You can configure product features such as color, size, width, height,&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1768","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1768"}],"version-history":[{"count":6,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1768\/revisions"}],"predecessor-version":[{"id":18078,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1768\/revisions\/18078"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}