Save up to 30% Learn more

Shopping cart

Magento 2 Add Category Attributes Programmatically

Magento 2 Category Attributes Programmatically is a timely topic so that you can work on custom magento 2 extension, for example, in the right way. Welcome here, Mageplaza Tutorial! The exact steps are listed with the specific script in PHP to create the category attributes. By the availability of the setup scripts, there is no need to waste time learning or modifying anything while you don’t have experience in coding. The only thing you need to do is copying and pasting the following snippets to your module. Here are the snippets that you can achieve immediately.

Overview of Adding Category Attribute Programmatically

  • Step 1: Create file InstallData.php
  • Step 2: Define the install() method
  • Step 3: Create the category attribute
  • Step 4: Display the category attribute

In this article, we will use the Mageplaza HelloWorld module to learn how to add a category attribute.

Step 1: Create file InstallData.php

We will start with the InstallData class which located in app/code/Mageplaza/HelloWorld/Setup/InstallData.php. The content for this file:

<?php
namespace Mageplaza\HelloWorld\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
	private $eavSetupFactory;

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

Step 2: Define the install() method

<?php
	
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
	
}

Step 3: Create the category attribute

<?php
namespace Mageplaza\HelloWorld\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
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,
			'mp_new_attribute',
			[
				'type'         => 'varchar',
				'label'        => 'Mageplaza Attribute',
				'input'        => 'text',
				'sort_order'   => 100,
				'source'       => '',
				'global'       => 1,
				'visible'      => true,
				'required'     => false,
				'user_defined' => false,
				'default'      => null,
				'group'        => '',
				'backend'      => ''
			]
		);
	}
}

Step 4: Display the category attribute

The category UI Component is rendered with configuration from the category_form.xml file. All files with that name are merged together. As a result, We will add a field by creating a category_form.xml file in the app/code/Mageplaza/HelloWorld/view/adminhtml/ui_component/ directory.

Here is a full example of adding a field under the “general” group. It is important to note that mp_new_attribute 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="general">
        <field name="mp_new_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">333</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">Mageplaza new attribute</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

All done, please run upgrade, flush cache and check the result.

  1. Flush Cache Run flush cache here

  2. Run upgrade command line:

Then go to Product > Categories from the admin to check the result. It will show like this:

create category attribute





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
category
attributes
programmatically
featured

Comments for Magento 2 Add Category Attributes Programmatically

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
69 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

People also searched for:

  • Magento 2 category attributes programmatically
  • Create category attributes in Magento 2
  • Magento 2 create category attributes
  • Creating category attributes programmatically
  • Searches related to Magento 2 Category Attributes
  • magento 2 create category attribute programmatically
  • magento 2.1 create category attribute
  • magento 2 create category programmatically
  • magento 2 add category attribute programmatically
  • magento 2.1 add category attribute
  • how to add new custom category attribute in magento
  • magento add category attribute programmatically
  • magento 2 custom category attribute