Here, In this post, you will understand how we can add Magento 2 custom category attributes programmatically (InstallSchema.php).
When we develop an online store in Magento 2 then sometimes we need this kind of functionality to add Category Attributes Programmatically.
We have developed a Magento 2 extension so that you can work on a custom Magento 2 module for custom yes/no attributes.
Just download that module or copy each and every file in your app/code and change your attribute title "Title Name" as per your requirements.
We have created two files which are InstallScheme.php and the second file is category_form.xml.
You can do copying and pasting the following snippets in your module. And the snippets that you can achieve immediately.
Add Category Attribute Programmatically in Magento 2
In this article, we will use the Wishusuces Yes No Custom Category Attributes module to learn how to add a category attribute.
Basic File for Yes/No Attribute in Magento 2
We have to create a minimum of four files to get the yes/no attribute in the category section in Magento 2.
Wishusucess/YesnoCustomCategoryAttributes/registration.php
Wishusucess/YesnoCustomCategoryAttributes/etc/module.xml
Wishusucess/YesnoCustomCategoryAttributes/Setup/InstallData.php
Wishusucess/YesnoCustomCategoryAttributes/view/adminhtml/ui_component/category_form.xml
Now we will check what content we need to add to all those files
app/code/Wishusucess/YesnoCustomCategoryAttributes/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Wishusucess_YesnoCustomCategoryAttributes', __DIR__ );
Now we have to create a module.xml file.
Wishusucess/YesnoCustomCategoryAttributes/etc/module.xml
<?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="Wishusucess_YesnoCustomCategoryAttributes" setup_version="1.0.0"> </module> </config>
InstallSchema.php for Yes/No Custom Category Attributes
app/code/Wishusucess/YesnoCustomCategoryAttributes/Setup/InstallData.php
<?php namespace Wishusucess\YesnoCustomCategoryAttributes\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_attributes', [ 'type' => 'int', 'label' => 'Custom Category Attributes', 'input' => 'boolean', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'visible' => true, 'default' => '0', 'required' => false, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'group' => 'Display Settings', ]); } }
How To Display The Custom Category Attributes
Magento 2 category_form.xml provides the options to add custom attributes in the category section. So here view/adminhtml/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
So here we have added these custom attributes in the category display setting.
These custom_category_attributes should match the ID of the attribute with InstallSchema.php.
app/code/Wishusucess/YesnoCustomCategoryAttributes/view/adminhtml/ui_component/category_form.xml
<?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="display_settings"> <field name="custom_category_attributes"> <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">Custom Category Attributes</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>
Download Yes/No Category Custom Attributes
On Github: Wishusucess/YesnoCustomCategoryAttributes
Related Post:
HelloWorld Extension: How to Create Hello World Module
Wishusucess AdminMenu: How to Create Magento 2 Admin Menu Module
Recommended Post:
Magento 2.4 Installation Guide: How to Install Magento 2.4.2
Magento Store: Best 36 Magento Websites Example in The World