When your Magento 2 stores is bulked with too many attribute sets or while importing from older Magento to a newer version, you may face some issues like older attributes cannot fit the newer versions or attribute sets cannot be managed manually. Creating new attribute set and assigning attributes to this newly created attribute set may be a cumbersome task if done manually as you have to follow local development to live publication process. What if you can automate creating attribute set and assign attributes to it programmatically in Magento 2?! It seems like saving efforts and time, right? Today, MageComp has come up with the simplest way to create attribute set programmatically & assign attributes to it in Magento 2.
All you need to do is, go to Vendor/Extension/Setup/InstallData.php and paste this and it will work like charm to create attribute set automatically.
1 |
<!--?php namespace Vendor\Extension\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Setup\CategorySetupFactory; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; class InstallData implements InstallDataInterface { private $eavSetupFactory; private $attributeSetFactory; private $attributeSet; private $categorySetupFactory; public function __construct(EavSetupFactory $eavSetupFactory, AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory ) { $this->eavSetupFactory = $eavSetupFactory; $this->attributeSetFactory = $attributeSetFactory; $this->categorySetupFactory = $categorySetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); // CREATE ATTRIBUTE SET $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $attributeSet = $this->attributeSetFactory->create(); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); $data = [ 'attribute_set_name' => 'YOUR_ATTRIBUTE_SET_NAME', 'entity_type_id' => $entityTypeId, 'sort_order' => 200, ]; $attributeSet->setData($data); $attributeSet->validate(); $attributeSet->save(); $attributeSet->initFromSkeleton($attributeSetId); $attributeSet->save(); // CREATE PRODUCT ATTRIBUTE $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'attribute_id', [ 'type' => 'varchar', 'label' => 'YOUR ATTRIBUTE LABEL', 'backend' => '', 'input' => 'text', 'wysiwyg_enabled' => false, 'source' => '', 'required' => false, 'sort_order' => 5, 'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE, 'used_in_product_listing' => true, 'visible_on_front' => true, 'attribute_set_id' => 'YOUR_ATTRIBUTE_SET_NAME', ] ); $setup->endSetup(); } } ?--> |
That’s it, if you have done everything properly, it will definitely create new attribute set with assigned attributes inside your Magento 2 store backend. Feel free to ask questions or send your feedback regarding this code through commenting.
Happy Coding!
Thank you, but php bin/magento setup:upgrade, it show Nothing to import. What did I miss
InstallData is only read when installing the module, so bin/magento setup:upgrade does not update.
Delete your YourVendor_YourModule from the setup_module table and run bin/magento setup:upgrade again