In this blog, we are going through how to add custom category attributes in Magento 2.

Why Create a Custom Category Attribute in Magento 2? Because in a category you need to add some external text, numbers, multiple choice categories, etc. you can create category attribute

In this blog, we create category attributes using the latest technology  “data/patch”.let’s start with how to create categories attribute.

⇒    First of all, you create “CategoryAttribute.php”  in this directory “Vendor_name/Module_name/Setup/Patch/Data/”.

<?php
/**
* @author CynoInfotech Team
* @package vendor_module
*/
declare (strict_types = 1);
namespace vendor\module\Setup\Patch\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Catalog\Model\Category;
/**
 * Class CategoryAttribute for Create Custom Category Attribute using Data Patch.
 */
class CategoryAttribute implements DataPatchInterface {
    /**
     * ModuleDataSetupInterface
     *
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * EavSetupFactory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory          $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * {@inheritdoc}
     */
    public function apply() {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(Category::ENTITY, 'category_attribute', [
            'type' => 'text',
            'label' => 'Custom Category Attribute',
            'input' => 'text',
            'default' => 0,
            'sort_order' => 5,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
            'visible_on_front' => true
        ]);
    }
    /**
     * {@inheritdoc}
     */
    public static function getDependencies() {
        return [];
    }
    /**
     * {@inheritdoc}
     */
    public function getAliases() {
        return [];
    }
}

⇒ After that you need to create “category_form.xml” in this directory “vendor_name/module_name/view/adminhtml/ui_component“.

<?xml version="1.0" ?>
<!--
/**
* @author CynoInfotech Team
* @package vendor_module
*/
-->
<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_attribute">
            <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">Custom Category Attribute</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

⇒ and last you need to run this command in the root directory

  • php -dmemory_limit=2G bin/magento setup:upgrade
  • php -dmemory_limit=2G bin/magento setup:static-content:deploy -f -j
  • php -dmemory_limit=2G bin/magento cache:flush

⇒ after checking the custom category created successfully.  If you have any questions regarding this blog let me know in the comment section.

Thank You !!!!

CynoInfotech was founded in 2019, in Ahmedabad, Gujrat. CynoInfotech is a Magento module development company that grow up over this year with outsourcing clients.