Here, we will discuss about how to add custom attribute in Magento 2 Category section.
I have created a custom module to add boolean type custom attribute.
Here, I have used MageMonkey as vendor and CustomCategorySettings as the module name.
Please follow below steps:
1. Create MageMonkey/CustomCategorySettings directories in app/code
2. Create registration.php file at app/code/MageMonkey/CustomCategorySettings/ and add following code:
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MageMonkey_CustomCategorySettings', __DIR__ ); |
3. Create module.xml file at app/code/MageMonkey/CustomCategorySettings/etc/ and add following code:
1 2 3 4 5 6 7 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="MageMonkey_CustomCategorySettings" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config> |
4. Create InstallData.php file at app/code/MageMonkey/CustomCategorySettings/Setup/ and add following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php namespace MageMonkey\CustomCategorySettings\Setup; use Magento\Framework\Module\Setup\Migration; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Setup\CategorySetupFactory; class InstallData implements InstallDataInterface { /** * Category setup factory * * @var CategorySetupFactory */ private $categorySetupFactory; /** * Init * * @param CategorySetupFactory $categorySetupFactory */ public function __construct(CategorySetupFactory $categorySetupFactory) { $this->categorySetupFactory = $categorySetupFactory; } /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); $categorySetup->removeAttribute( \Magento\Catalog\Model\Category::ENTITY, 'CUSTOM_ATTRIBUTE' ); $categorySetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'CUSTOM_ATTRIBUTE', [ 'type' => 'int', 'label' => 'CUSTOM ATTRIBUTE LABEL', 'input' => 'boolean', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'required' => false, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'group' => 'Custom Settings', ] ); $installer->endSetup(); } } |
5. Create category_form.xml file at app/code/MageMonkey/CustomCategorySettings/view/adminhtml/ui_component/ and add following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="UTF-8"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="custom_settings"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="label" xsi:type="string">Custom Settings</item> <item name="collapsible" xsi:type="boolean">true</item> <item name="sortOrder" xsi:type="number">99</item> </item> </argument> <field name="CUSTOM_ATTRIBUTE"> <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 ATTRIBUTE LABEL</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> |
6. Run following commands to enable this custom module.
php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush
Now, you can see in Magento 2 Admin > Catalog > Category
There will be a tab group “Custom Settings” in admin category as shown below.
[crayon-63d3e2b4657cc689557756/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.