Add Custom Category Attribute Programmatically Magento 2

In this article, you will learn how we can add custom category attribute programmatically in Magento 2.

Adding custom category attributes in Magento 2 is automatically displayed in the admin panel when you will use the below setup InstallSchema.php file and category_form.xml file for UI component and then run setup: upgrade command.

So here we will have complete control over the input type and the other entity of the attribute. So when you will copy this code for your online Magento 2 shopping store then you can simply add the label like"Your Label Name" and custom attribute identifier "your_custom_category_identifier".

 

Create Category Attribute in Magento 2

in order to create the custom category attribute in Magento 2, you have to use the install script that will help you to create creates a custom category attribute. But if you have already a custom attribute then don't need to install the script.

Now just create a module for the Magento category attribute.

app/code/Wishuscess/Attributes/registration.php

app/code/Wishuscess/Attributes/etc/module.xml

app/code/Wishuscess/Attributes/Setup/InstallData.php

app/code/Wishuscess/Attributes/view/adminhtml/ui_component/category_form.xml

 

Registration of Attribute Module

Here you first have to register the module through the below file.

app/code/Wishuscess/Attributes/registration.php

<?php
/*
* @Author Hemant Singh
* @Developer Hemant Singh
* @Module Wishuscess_Attributes
* @copyright Copyright (c) Wishusucess (http://www.wishusucess.com/)
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishuscess_Attributes',
__DIR__
);

 

Declare Module XML File of Module

Now here you have to give the module name and scheme version of the Wishuscess_Attributes Magento 2 extension.

app/code/Wishuscess/Attributes/etc/module.xml

<?xml version="1.0"?>
<!--
/*
* @Author Hemant Singh
* @Developer Hemant Singh
* @Module Wishuscess_Attributes
* @copyright Copyright (c) Wishusucess (http://www.wishusucess.com/)
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Wishuscess_Attributes" setup_version="2.0.0">
<sequence>
<module name="Magento_Backend"/>
</sequence>
</module>
</config>

 

Add Custom Category Attribute Setup File

Magento 3 has its own syntax for custom attribute creation so we have to follow the syntax to create the custom attributes.

app/code/Wishuscess/Attributes/Setup/InstallData.php

<?php
/*
* @Author Hemant Singh
* @Developer Hemant Singh
* @Module Wishuscess_Attributes
* @copyright Copyright (c) Wishusucess (http://www.wishusucess.com/)
*/
namespace Wishuscess\Attributes\Setup;

use Magento\Framework\Setup\{
ModuleContextInterface,
ModuleDataSetupInterface,
InstallDataInterface
};

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{
private $eavSetupFactory;

public function __construct(EavSetupFactory $eavSetupFactory) {
$this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'custom_category_attribute', [
'type' => 'varchar',
'label' => 'Category Custom Description',
'input' => 'textarea',
'source' => '',
'visible' => true,
'wysiwyg_enabled' => true,
'is_html_allowed_on_front' => true,
'visible' => true,
'required' => false,
'visible_on_front' => false,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'Content',
]);
}
}

 

Display The Category Attribute in Magento 2

Here we will use the category UI Component to display the custom category attribute in Magento admin so this file will rendered configuration from the category_form.xml file.

Now in the category_form.xml file, you have to give the field name like below code

<field name="custom_category_attribute" template="ui/form/field" sortOrder="50" formElement="wysiwyg">

You can add a field by creating a category_form.xml file like this  app/code/Wishuscess/Attributes/view/adminhtml/ui_component/category_form.xml directory in your module.

In another step, we have to decide the fieldset so like where we want to display that attribute, and then attribute_id should match the ID of the attribute that you created in the install script.

<?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="content">
<field name="custom_category_attribute" template="ui/form/field" sortOrder="50" formElement="wysiwyg">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="wysiwygConfigData" xsi:type="array">
<item name="height" xsi:type="string">100px</item>
<item name="add_variables" xsi:type="boolean">false</item>
<item name="add_widgets" xsi:type="boolean">false</item>
<item name="add_images" xsi:type="boolean">true</item>
<item name="add_directives" xsi:type="boolean">true</item>
</item>
<item name="source" xsi:type="string">category</item>
</item>
</argument>
<settings>
<label translate="true">Category Custom Description</label>
<dataScope>custom_category_attribute</dataScope>
</settings>
<formElements>
<wysiwyg class="Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg">
<settings>
<rows>8</rows>
<wysiwyg>true</wysiwyg>
</settings>
</wysiwyg>
</formElements>
</field>
</fieldset>
</form>

 

Now, Run Following Command:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deplpy -f

php bin/magento cache:clean

 

Add Custom Category Attribute

Related Post:

Custom Shipping Text Filed: Show Custom Text Magento 2

Out Of Stock Product: Show on The Front end in Magento 2

Share Product on WhatsApp: With Products Image And URL in Magento 2

 

Recommended Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Magento Store: Best 36 Magento Websites Example in The World

SEO Packages: How Much Do SEO Packages Cost in India, SEO Pricing