
Add Custom Category Attribute Magento2 Extension Free
USER GUIDE
- How to Register Module in Magento 2
- Tell Magento Which Extesions to Be Included
- Register Attribute in our Core
- Add Code for showing Attribute to set value from Admin Dashboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php /* * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: zaheercena@gmail.com * Number: +92-301-1000201 * Copyright: MIT Licence(Open to edit) */ ?> <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Cozmot_CategoryAttribute', __DIR__ ); |
After Registering Tell Magento to Load Our Extension
1 2 3 4 5 6 7 |
<?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="Cozmot_CategoryAttribute" setup_version="1.0.0"> </module> </config> |
Not it’s time to Implement Logic, Here are we Registering our Attribute for Category which will be Boolean Formed.
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 |
<?php /* * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: zaheercena@gmail.com * Number: +92-301-1000201 * Copyright: MIT Licence(Open to edit) */ ?> <?php namespace Cozmot\CategoryAttribute\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, 'cozmotmobilemenu', [ 'type' => 'int', 'label' => 'Cozmot Category Attribute', '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', ]); } } |
Now we have to add Admin side option in Category options to set value of our Attribute.
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 |
<!-- * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: zaheercena@gmail.com * Number: +92-301-1000201 * Copyright: MIT Licence(Open to edit) --> <?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="cozmotmobilemenu"> <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">Cozmot Category Attribute</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> |
Hurrayyyy… That’s all needed Now run Following commands according to your Installation.
1 2 3 4 |
php bin/magento setup:upgrade php bin/magento c:c |
]]>