How can I add an attribute to all products? Now i am adding this attribute to all sets. Is there a way it can be done faster?


Please try with create custom scripts in root like customscript.php


ini_set('display_errors', 1);

$ATTRIBUTE_CODE = 'attribute_code';
$ATTRIBUTE_GROUP = '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);

/* 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);

than run command php customscript.php or http://siteurl.com/customscript.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.