How To Create Custom Attribute Set Programmatically In Magento 2

In this post I'll let you now how to Create Custom Attribute Set Using Installer Script In Magento 2.

For that you need to follow some steps.

First, you need to create a installer file InstallData.php in Vendor\CustomModule\Setup and then add following code.

saveCopyzoom_out_map
<?php namespace Vendor\ModuleName\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Catalog\Setup\CategorySetupFactory; class InstallData implements InstallDataInterface { private $attributeSetFactory; private $categorySetupFactory; public function __construct( AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory ) { $this->attributeSetFactory = $attributeSetFactory; $this->categorySetupFactory = $categorySetupFactory; } public function install( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) { $setup->startSetup(); $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $attributeSet = $this->attributeSetFactory->create(); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); // Default attribute set Id $data = [ 'attribute_set_name' => 'Customattributeset', //attribute set name 'entity_type_id' => $entityTypeId, 'sort_order' => 50, ]; $attributeSet->setData($data); $attributeSet->validate(); $attributeSet->save(); $attributeSet->initFromSkeleton($attributeSetId)->save(); // based on default attribute set } }

We are done with that. Now run setup:upgrade command and after complete setup upgrade process check attribute set section on admin and you see new attribut set with name Customattributeset.

Bingo!!

You are successfully done with that. Bookmark it for your future reference. Do comment below if you have any other questions or doubts on this post.

P.S. Do share this note with your team.

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off