How to Programmatically Assign Attribute to All Attribute Sets in Magento 2
Magento 2 provides 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.
Specific product attribute assigns to the attribute set, 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 Magento 2 Admin Panel > Stores > Attributes > Attribute Set.
Now, what if you want to assign an attribute to all attribute sets? Let’s assume you have fifteen attribute sets, and you want to assign your attribute to all attribute sets! Won’t it be tiresome and too much effort to assign attributes to all attribute sets from admin individually?
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!
In such a scenario, use the below method to programmatically assign attribute to all attribute sets in Magento 2.
Method to Programmatically Assign Attribute to All Attribute Sets in Magento 2
- If you already created a product attribute then just assign it to all attribute sets.
Create a PHP file “AssignAttributes.php” in your Magento 2 root directory.Change the value for $ATTRIBUTE_CODE and $ATTRIBUTE_GROUP.12345678910111213141516171819202122232425262728293031323334353637<?phperror_reporting(E_ALL);ini_set('display_errors', 1);$ATTRIBUTE_CODE = 'attribute_text';$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);}} - When you create a custom module and a custom product attribute then assign this attribute to all attribute sets.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667<?phpnamespace Vendor\Module\Setup;use Magento\Eav\Setup\EavSetupFactory;use Magento\Framework\Setup\InstallDataInterface;use Magento\Framework\Setup\ModuleContextInterface;use Magento\Framework\Setup\ModuleDataSetupInterface;class InstallData implements InstallDataInterface{protected $eavSetupFactory;public function __construct(EavSetupFactory $eavSetupFactory){$this->eavSetupFactory = $eavSetupFactory;}public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'custom_product_attribute');$eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY,'custom_product_attribute',['type' => 'varchar','backend' => '','frontend' => '','label' => 'Attribute Label','input' => 'text','class' => '','source' => '','global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,'group' => 'Attribute Group','visible' => false,'required' => false,'user_defined' => false,'default' => '','searchable' => false,'filterable' => false,'comparable' => false,'visible_on_front' => false,'used_in_product_listing' => false,'unique' => false,'apply_to' => '']);$ATTRIBUTE_GROUP = 'General'; // Attribute Group Name$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);$allAttributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);foreach ($allAttributeSetIds as $attributeSetId) {$groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, $ATTRIBUTE_GROUP);$eavSetup->addAttributeToGroup($entityTypeId,$attributeSetId,$groupId,'custom_product_attribute',null);}}}
That’s it.
Any doubts in the above solution? If so, please mention them in the Comments section below.
I’d be happy to help.
Also, do share the post with Magento Community via social media.
Thank you.