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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $params = $_SERVER; $bootstrap = Bootstrap::create(BP, $params); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resourceConnection = $objectManager->create('\Magento\Framework\App\ResourceConnection'); $attributeSet = $objectManager->create('\Magento\Eav\Model\Entity\Attribute\Set'); $entity_type = $objectManager->create('\Magento\Eav\Model\Entity\Type'); $productFactory = $objectManager->create('\Magento\Catalog\Model\ProductFactory'); $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.
[crayon-63d3d4c71104c102663848/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.