How to form a new attribute for category in Magento 2

Form a new attribute for category

There are few ways to broaden the functionality of Magento categories, which will show you how to do it.

By modifying and adding some of the table directly, you can do it, but if you do not know what you’re doing, you may waste time. So, you can follow these steps and create custom type attributes within minutes. If you want to create a custom type attribute for your ecommerce site you can try the following steps

Step 1: Go to your model and create the Setup folder, create InstallData.php within it, press this complete code   app/code/[Name_space]/[Your_module]/Setup/InstallData.php

<?php

namespace [Name_space]\[Your_module]\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, 'new_attribute');

     $categorySetup->addAttribute(

     \Magento\Catalog\Model\Category::ENTITY, 'new_attribute', [

      'type' => 'int',

      'label' => 'New Atrribute ',

      'input' => 'select',

      'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

      'required' => false,

      'sort_order' => 100,

      'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,

      'group' => 'General Information',

     ]

     );

     $installer->endSetup();

}

}

Step 2: Include category_form.xml in app/code/[Name_space]/[Your_module]/view/adminhtml/ui_component and push

</span>

<?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="general">

     <field name="new_attribute">

         <argument name="data" xsi:type="array">

         <item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>

             <item name="config" xsi:type="array">

                 <item name="sortOrder" xsi:type="number">60</item>

                 <item name="dataType" xsi:type="string">string</item>

                 <item name="formElement" xsi:type="string">select</item>

                 <item name="label" xsi:type="string" translate="true">New Attribute</item>

             </item>

         </argument>

     </field>    

</fieldset>

</form>

<span style="line-height:13px">


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *