Add Custom Category Attributes In Magento 2

In the previous article, we learned about How to get product collection in Magento 2. Today, we will introduce you about Add custom category attributes in Magento 2.

I think you should read How To Create New Theme In Magento 2 first.

Create the attributes

We’ll use our module to make script update category attributes.

Suppose current our module version is 1.0.0, create file:

app/code/<vendor_name>/<module_name>/Setup/UpgradeSchema.php should look like:

<?php
namespace Magetop\Themes\Setup;
 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
 
class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if ($context->getVersion() && version_compare($context->getVersion(), '1.0.1') < 0) {
  
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
  
            $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'cat_featured',
            [
                'type' => 'int',
                'label' => 'Is featured',
                'input' => 'boolean',
                'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'required' => false,
                'sort_order' => 10,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'wysiwyg_enabled' => true,
                'default' => '0',
                'group' => 'General Information'
            ]
        );
        } 
        $setup->endSetup();
    }
}

Show its in backend

Create file app/code/<vendor_name>/<module_name>/view/adminhtml/ui_component/category_form.xml should look like:

<?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="cat_featured" sortOrder="41">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="label" xsi:type="string" translate="true">Is featured</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="string">1</item>
                        <item name="false" xsi:type="string">0</item>
                    </item>
                    <item name="default" xsi:type="number">0</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Then update version for our module. Update file app/code/<vendor_name>/<module_name>/etc/module.xml should look like:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magetop_Themes" setup_version="1.0.1"/>
</config>

After open command prompt and change directory to your magento install directory

Run the following command in command prompt:

php bin/magento setup:upgrade

php bin/magento cache:clean

Then go to backend refresh cache to see your changes applied.

We updated our file structure looks as follows:

app/code/Magetop/
  ├── Themes/
  │   ├── Setup/
  │   │     ├── UpgradeSchema.php
  │   ├── etc/
  │   │     ├── module.xml
  │   ├── view/
  │   │     ├── adminhtml/
  │   │     │   ├── ui_component/
  │   │     │   │   ├── category_form.xml

Good luck!

This is Add custom category attributes in Magento 2.

You can see the next post: Make Default System Config Value In Magento 2.

Follow us for more helpful tutorial!

We hope this is useful blog for you.

Thank you for reading!

2.7 3 votes
Article Rating

Adam Roger

CEO and Founder of Magetop. A friend, a husband and a dad of two children. Adam loves to travel to experience new cultures and discover what is happening with ecommerce all around the world.

Leave a Reply or put your Question here

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x