Magento 2: Create Custom Attribute Set Using Data-Patch

Create Attribute Set

We often need to create a custom attribute set using the data-patch. Since Install Data & Upgrade Data have been deprecated and the Data patch has been introduced. We need data patches to create EAV attributes as well as attribute set programmatically.

The attributes are organized into groups that determine where they appear in the product record. Your store comes with an initial attribute set, called default, that includes a set of commonly-used attributes. If you would like to add only a small number of attributes, you can add them to this default attribute set. However, if you sell products that require specific types of information, such as cameras, it might be better to create a dedicated attribute set that includes the specific attributes that are needed to describe the product.

Let’s see How we can Create an attribute set using data-patch.

<?php
namespace Vendor\Module\Setup\Patch\Data;

use Exception;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory;
use Psr\Log\LoggerInterface;


class CreateCustomAttributeSetPatch implements DataPatchInterface
{
    private const ATTRIBUTE_SET_SORT_ORDER = 100;

    /**
     * @var ModuleDataSetupInterface
     */
   private ModuleDataSetupInterface $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
   private EavSetupFactory $eavSetupFactory;

    /**
     * @var SetFactory
     */
   private SetFactory $attributeSetFactory;

    /**
     * @var LoggerInterface
     */
   private LoggerInterface $logger;

    /**
     * Constructor
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     * @param SetFactory $attributeSetFactory
     * @param LoggerInterface $logger
     */
   public function __construct(
       ModuleDataSetupInterface $moduleDataSetup,
       EavSetupFactory $eavSetupFactory,
       SetFactory $attributeSetFactory,
       LoggerInterface $logger
   ) {
       $this->moduleDataSetup = $moduleDataSetup;
       $this->eavSetupFactory = $eavSetupFactory;
       $this->attributeSetFactory = $attributeSetFactory;
       $this->logger = $logger;
   }

    /**
     * Creating new attribute set
     *
     * @return $this|CreateCustomAttributeSetPatch
     * @throws Exception
     */
   public function apply():self
   {
       try {
           $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

           $attributeSet = $this->attributeSetFactory->create();
           $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
           $attributeSetId = $eavSetup->getDefaultAttributeSetId($entityTypeId);

           $data = [
               'attribute_set_name' => 'Custom Attribute Set',
               'entity_type_id' => $entityTypeId,
               'sort_order' => self::ATTRIBUTE_SET_SORT_ORDER,
           ];

           $attributeSet->setData($data);
           $attributeSet->validate();
           $attributeSet->save();
           $attributeSet->initFromSkeleton($attributeSetId);
           $attributeSet->save();

       } catch (LocalizedException|Exception $ex) {
           $this->logger->error("Something is wrong while creating Custom Attribute Set ". $ex->getMessage());
       }

       return $this;
   }

    /**
     * @inheirtDoc
     */
   public static function getDependencies():array
   {
       return [];
   }

    /**
     * @inheirtDoc
     */
   public function getAliases():array
   {
       return [];
   }

}

Also check out the post: https://sbdevblog.com/magento-2-how-to-get-logged-in-customer-id/

Download Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *