We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Magento provides the add product attribute set from the backend, but there is no way to add multiple attribute sets at once because, in the backend, we have to add one by one, so here I’m sharing the script for adding numerous sets at once.

    Here is the script which is directly use from Magento root path:

    <?php
    
    use MagentoFrameworkAppBootstrap;
    
    require __DIR__ . '/app/bootstrap.php';
    $params = $_SERVER;
    $bootstrap = Bootstrap::create(BP, $params);
    $obj = $bootstrap->getObjectManager();
    $state = $obj->get('MagentoFrameworkAppState');
    $state->setAreaCode('frontend');
    
    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
    $resourceConnection = $objectManager->create('MagentoFrameworkAppResourceConnection');
    $attributeSet = $objectManager->create('MagentoEavModelEntityAttributeSet');
    $entity_type = $objectManager->create('MagentoEavModelEntityType');
    $productFactory = $objectManager->create('MagentoCatalogModelProductFactory');
    $setsArray = array("Cup", "Size", "BandSize", "Color"); //You have to put here your require attribute sets
    if (!empty($setsArray)) {
        $sortOrder = 200;
        foreach ($setsArray as $checkSet) {
            $connection = $resourceConnection->getConnection();
            $select = "SELECT * from eav_attribute_set WHERE attribute_set_name = '" . $checkSet . "'";
            $result = $connection->fetchAll($select);
            if (count($result) == 0) {
                $entityType = $entity_type->loadByCode('catalog_product');
                $defaultSetId = $productFactory->create()->getDefaultAttributeSetid();
                $data = [
                    'attribute_set_name' => $checkSet,
                    'entity_type_id' => $entityType->getId(),
                    'sort_order' => $sortOrder,
                ];
    
                $attributeSet->setData($data);
                $attributeSet->validate();
                $attributeSet->save();
                $attributeSet->initFromSkeleton($defaultSetId);
                $attributeSet->save();
            }
            $sortOrder++;
        }
    
        echo 'Attribute Sets Process Finished';
    }

    First, prepare the above script in any PHP file of your Magento root then run that PHP file by CLI, and script execution will be started.

    After finishing the file execution you can clean the Magento cache and see the result in the backend. All attribute sets will get successfully added.

     

    field_5bfb909c5ccae

      Get a Free Quote