Blog Page

blog details

12 Sep 2022

Add Custom Category Attribute Magento2 Extension Free

Here is our Free Extension of Magento to get Created Custom Category Attribute, you can change Name, Customize Type of attribute or just install in.

USER GUIDE

  1. How to Register Module in Magento 2
  2. Tell Magento Which Extesions to Be Included
  3. Register Attribute in our Core
  4. Add Code for showing Attribute to set value from Admin Dashboard  
<?php
/*
* Company Name: Cozmot Inc
* Website: https://cozmot.com
* Writer Name: Zaheer Ahmed
* Contact: [email protected]
* 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
<?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.
<?php
/*
* Company Name: Cozmot Inc
* Website: https://cozmot.com
* Writer Name: Zaheer Ahmed
* Contact: [email protected]
* 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.
<!--
* Company Name: Cozmot Inc
* Website: https://cozmot.com
* Writer Name: Zaheer Ahmed
* Contact: [email protected]
* 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.
php bin/magento setup:upgrade
php bin/magento c:c