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

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

Create the PHP file in your Magento 2 root directory. If the product attributes are already created then just assign them to the attribute sets.

<?php

error_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
);
}
}

Change the value of $ATTRIBUTE_CODE and $ATTRIBUTE_GROUP according to your attribute code and group.

When you want to create the custom module and custom attribute then assign them to attribute sets.

<?php

namespace 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
);
}
}
}
If you are looking for Magento Developers, visit Magento Developer Agency.
Related Products