How to create a Category Attribute using Data Patches in Magento 2

In this tutorial, we will learn how to create category attributes using data patches. In Magento 2.3.x, Magento has introduces Declarative Schema approach with this approach comes the data and schema patches. Data patch is a class that contains data modification instructions.

Here, we can use data patch to add attributes to category Programmatically.

How you can create Data Patch with Magento 2.3.x:

A data patch defined in Vendor/Module/Setup/Patch/Data/PatchClassName.php and it will implement \Magento\Setup\Model\Patch\DataPatchInterface interface.

Perform following steps to create category attribute using data patch in Magento 2:

Step 1: First, I assume that you have created a simple module with required files.

Step 2: To create a custom category attribute ‘subtitle’ using data patch AddSubTitleCategoryAttribute.php class file at path app/code/Mageclues/CategoryAttr/Setup/Patch/Data/

<?php

namespace Mageclues\CategoryAttr\Setup\Patch\Data;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Model\Category;


class AddSubTitleCategoryAttribute implements DataPatchInterface
{

    protected $_moduleDataSetup;
    protected $_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, 'category_subtitle', [
            'type' => 'text',
            'label' => 'Sub Title',
            'input' => 'text',
            'default' => '',
            'sort_order' => 3,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
            'visible_on_front' => true
        ]);
    }

    public static function getDependencies()
    {
        return [];
    }
   
    public function getAliases()
    {
        return [];
    }
}

Step 3: Now, to add an attribute in category form, We need to create category_form.xml at app/code/Mageclues/CategoryAttr/view/adminhtml/ui_component/ and paste the below code snippet in it.

<?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="category_subtitle">
            <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">Sub Title</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Step 4: In Last, just run following command from CLI.


php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean

Once you run php bin/magento setup:upgrade command then data patch is executed and the EAV attribute is created in SQL table. All patches which are successfully executed, Magento inserts a patch record into the patch_list database table with the value of the patch_name field being the value of our patch.

We hope this tutorial is useful for you. Please leave a comment if you have any questions.

Thank you):

Related Posts

Leave a Reply

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