Mage Check Facebook icon Mage Check Linkedin icon

Magento How to Programmatically Create Category Attribute

Step 1: Define Function

Magento I (2007 - 2014)

<?xml version='1.0'?> <config>   <modules>     <MageCheck_CategoryAttribute>       <version>0.0.1</version>     </MageCheck_CategoryAttribute>   </modules>   <global>     <resources>       <customcatattrib_setup>         <setup>           <module>MageCheck_CategoryAttribute</module>           <class>Mage_Eav_Model_Entity_Setup</class>         </setup>         <connection>           <use>default_setup</use>         </connection>       </customcatattrib_setup>     </resources>   </global> </config>

File path: app/code/local/MageCheck/CategoryAttribute/etc/config.xml

Magento 2 Open Source (2014 onward)

namespace MageCheck\Tutorial\Setup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\InstallDataInterface; class InstallData implements InstallDataInterface {   private $eavSetupFactory;   public function __construct(EavSetupFactory $eavSetupFactory)   {     $this->eavSetupFactory = $eavSetupFactory;   }   public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)   {     $setup->startSetup();     $this->createCategoryAttribute($setup);     $setup->endSetup();   }   public function createCategoryAttribute($setup)   {     // ...   } }

Step 2: Create Function

Magento I (2007 - 2014)

$installer = $this; $installer->startSetup(); $attribute = [   'type' => 'text',   'label'=> 'Extension Attribute',   'input' => 'textarea',   'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,   'visible' => true,   'required' => false,   'user_defined' => true,   'default' => '',   'group' => 'General Information' ]; $installer->addAttribute('catalog_category', 'extension_attribute', $attribute); $installer->endSetup();

File path: app/code/local/MageCheck/CategoryAttribute/sql/customcatattrib_setup/mysql4-install-0.0.1.php

Magento 2 Open Source (2014 onward)

public function createCategoryAttribute($setup) {   $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);   $eavSetup->addAttribute(     \Magento\Catalog\Model\Category::ENTITY,     'extension_attribute',     [       'type' => 'text',       'label' => 'Extension Attribute',       'input' => 'textarea',       'required' => false,       'sort_order' => 4,       'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,       'wysiwyg_enabled' => true,       'is_html_allowed_on_front' => true,       'group' => 'General Information'     ]   ); }
Custom
This is the sample way to Programmatically create Category Attribute in Magento!
Quote