0

I want to add more than 50 Product Attributes in the bulk. Please let me know the best way to do it in Magento 2.

1 Answer 1

1

Can you try using below script and create bulk product attributes:-

<?php

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$eavSetupFactory = $objectManager->create('\Magento\Eav\Setup\EavSetupFactory');
$moduleSetupFactory = $objectManager->create('\Magento\Framework\Setup\ModuleDataSetupInterface');
$eavSetup = $eavSetupFactory->create(['setup' => $moduleSetupFactory]);

$attributes = [
        [
            'attribute_code' => 'new_attribute_1',
            'frontend_input' => 'text',
            'frontend_label' => 'New Attribute 1',
            'backend_type' => 'varchar',
            'is_required' => 0,
            'is_user_defined' => 1,
            'is_global' => 0,
            'is_searchable' => 1,
            'is_filterable' => 1,
            'is_comparable' => 1,
            'is_visible_on_front' => 1,
            'is_html_allowed_on_front' => 1,
            'used_in_product_listing' => 1,
            'used_for_sort_by' => 1,
        ],
        [
            'attribute_code' => 'new_attribute_2',
            'frontend_input' => 'text',
            'frontend_label' => 'New Attribute 2',
            'backend_type' => 'varchar',
            'is_required' => 0,
            'is_user_defined' => 1,
            'is_global' => 0,
            'is_searchable' => 1,
            'is_filterable' => 1,
            'is_comparable' => 1,
            'is_visible_on_front' => 1,
            'is_html_allowed_on_front' => 1,
            'used_in_product_listing' => 1,
            'used_for_sort_by' => 1,
        ],
        // add more attributes as needed
    ];

foreach ($attributes as $attribute) {
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        $attribute['attribute_code'],
        [
            'type' => $attribute['backend_type'],
            'label' => $attribute['frontend_label'],
            'input' => $attribute['frontend_input'],
            'required' => $attribute['is_required'],
            'global' => $attribute['is_global'],
            'user_defined' => $attribute['is_user_defined'],
            'searchable' => $attribute['is_searchable'],
            'filterable' => $attribute['is_filterable'],
            'comparable' => $attribute['is_comparable'],
            'visible_on_front' => $attribute['is_visible_on_front'],
            'html_allowed_on_front' => $attribute['is_html_allowed_on_front'],
            'used_in_product_listing' => $attribute['used_in_product_listing'],
            'used_for_sort_by' => $attribute['used_for_sort_by'],
            'unique' => false,
            'apply_to' => '',
        ]
    );
}

I tried and it's working fine.

Let me know if any query.

THANKS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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