How to create category attributes using a data patch in Magento 2

When you are selling various products in the store, you will need to allow your customers to select their color, size, quantity, etc. However, you will need to add Magento product attributes to provide this functionality. That's why Magento 2 facilitates the admin to add custom product attribute from the backend. So in today's solution series, we have covered the topic of how to create category attributes using a data patch in Magento 2

Previously, Magento 2 uses InstallData and UpgradeData files to add data to the custom tables or default tables. Although, the Magento 2.3 update has replaced these files with DataPatch. Moreover, a data patch is a class that remembers all the instructions for data modification. So, Data patches help to add Magento custom product attributes in Magento 2 easily by just creating a single PHP file.

Steps to Create Category Attributes Using a Data Patch in Magento 2

In our article, we will learn how to create category attributes with the help of a data patch in Magento 2. So let's see step by steps guide as follows. Here let's assume that you have already created one simple module in your store.

Step 1: Firstly, you have to create a CustomAttribute.php file to create a custom category attribute in Magento 2. Create a CustomAttribute.php file at app\code\Vendor\Extention\Setup\Patch\Data. Now paste the below code.

<?php
namespace Vendor\Extention\Setup\Patch\Data;
 
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;
 
class CustomAttribute implements DataPatchInterface
{
   private $_moduleDataSetup;
 
   private $_eavSetupFactory;
 
   public function __construct(
       ModuleDataSetupInterface $moduleDataSetup,
       EavSetupFactory $eavSetupFactory
   ) {
       $this->_moduleDataSetup = $moduleDataSetup;
       $this->_eavSetupFactory = $eavSetupFactory;
   }
 
   public function apply()
   {
       /** @var EavSetup $eavSetup */
       $eavSetup = $this->_eavSetupFactory->create(['setup' => $this->_moduleDataSetup]);
 
       $eavSetup->addAttribute(Category::ENTITY, 'rh_cat_attr', [
                  'type' => 'text',
                  'label' => 'RH Custom Category Attribute',
                  'input' => 'text',
                  'default' => 0,
                  'sort_order' => 5,
                  'global' => ScopedAttributeInterface::SCOPE_STORE,
                  'group' => 'General Information',
                  'visible_on_front' => true
                ]);
   }
 
   public static function getDependencies()
   {
       return [];
   }
 
   public function getAliases()
   {
       return [];
   }
 
   public static function getVersion()
   {
      return '1.0.0';
   }
}

Step 2: Then, you will need to create a category_form.xml at app/code/Vendor/Extension/view/adminhtml/ui_component/ to add an attribute in the category form and copy-paste the below code.

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="rh_cat_attr">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="sortOrder" xsi:type="number">100</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" translate="true" xsi:type="string">Milople Custom Category Attribute</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Step 3: After that, run the following command in cmd.

php bin/magento setup:upgrade

That's all.

Wrapping Up

Hope this solution series article about how to create category attributes using a data patch in Magento 2 helps you. Create category attributes using a data patch effectively. However, if you still facing any errors or warnings, don't hesitate to contact us or let us know in the comment section. Additionally, if you find this blog post useful then share it with your developer friends on the Magento community via social media. You can check out our Magento 2 Customer Attributes.

Stay tuned:)