Magento 2 add custom category attributes

Please read first post Magento 2.3 create simple module to make configure opitons for theme.

Create the attributes

We’ll use our module to make script udpate 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 Magebay\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() &amp;&amp; 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="Magebay_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 reresh cache to see your changes applied.

We was update our file structure looks as follows:

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

For next post we’ll make default system config value.