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.
AI-Powered Recommended Articles
Magento 2 Create Category Attribute Programmatically
Learn how to create custom category attributes programmatically in Magento 2 for advanced category configurations.
Magento 2 Add Product Attribute Programmatically
Learn how to programmatically add product attributes in Magento 2 for enhanced product management.
Magento 2 Remove Product Attribute Programmatically
Learn how to remove product attributes programmatically in Magento 2 to streamline product management.
Magento 2 Add Product Attribute with Options Programmatically
Discover how to add product attributes with options programmatically in Magento 2 for flexible product configurations.
Magento 2 Add Customer Attribute Programmatically
Learn how to add custom customer attributes programmatically in Magento 2 for customized customer data.
Magento 2 Implement Declarative Schema in Custom Module.
A guide on implementing declarative schema in your custom Magento 2 modules to improve database management and scalability.