How To Create Custom Product Attribute Set From DataPatch Installer In Magento 2
How To Create Custom Product Attribute Set From DataPatch Installer In Magento 2- Here I am going to explain you how to create custom product attribute set from dataPatch installer.
If you want to create a custom product attribute set then create a file “AddCustomAttributeSet.php” in Vendor\CustomModule\Setup\Patch\Data and then add following code snippet.
<?php
namespace Vendor\ModuleName\Setup\Patch\Data;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Catalog\Setup\CategorySetupFactory;
class AddCustomAttributeSet implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var CategorySetupFactory
*/
private $categorySetupFactory;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param AttributeSetFactory $attributeSetFactory
* @param CategorySetupFactory $categorySetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
AttributeSetFactory $attributeSetFactory,
CategorySetupFactory $categorySetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->attributeSetFactory = $attributeSetFactory;
$this->categorySetupFactory = $categorySetupFactory;
}
/**
* @inheritdoc
*/
public function apply()
{
$setup = $this->moduleDataSetup;
$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' => 'Custom_Product_attribute_set', // custom 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
}
/**
* @inheritdoc
*/
public static function getDependencies()
{
return [];
}
/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}
}
Categories:
Magento2
View Comments
Comment or Ask a Question
Quick Links