Home » Magento7 August 2010

Magento: Adding category attributes

Here, I will be showing you how you can add attributes for your categories in Magento.

From the admin panel, you can only add attributes to product. To add attributes to category, you need to write sql query in your phpmyadmin or a better way would be creating a new custom module and adding attributes to category from the mysql setup file of the module.

By mysql setup file, I mean the file inside the directory YourNamespace/YourModule/sql/yourmodule_setup/

Create a new module and write the following in your module’s mysql setup file.

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$setup->addAttribute('catalog_category', 'my_attribute', array(
	'group'     	=> 'General',
	'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'My Attribute',		  
	'backend'       => '',
	'visible'       => 1,	
	'required'		=> 0,
	'user_defined' => 1,	
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,   	
));

$installer->endSetup();

The above code will create a category with the code ‘my_attribute‘ and lable ‘My Attribute‘. You can view it under Admin Panel –> Catalog –> Manage Categories –> General Information

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: Adding category attributes

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Yohanes

    What’s the filename I should save this php as?

    Thank you :)

  • siddharth

    Hi mukesh,

    I need to add attribute like image attribute of category, can you pls provide me source code regarding this ?

    Tbanks,
    Siddharth

  • fabien @ arboresys-conseil

    Hi,

    Thanks for sharing.
    I was able to define mys new attribute, but I missed the ‘group’ information to be able to see it in administration !

    @Yohanes : it’s defined in app/code/local///sql/_setup/mysql4-install-
    i.e : app/code/local/Arboresys/Mymodule/sql/Mymodule_setup/mysql4-install-1.0.0.php

  • satya k

    I am new to magento, I am integrating our site with magento…I have 2 website in Magento Admin panel..System–>Manage Stores–>create website,I have created 2 website and there corresponding view and store as well..I am able to assign categories using webservice request(as it requires category id i called the category tree service and fetched id).It is working fine,but how to assign websites using website request,I am unable to find a service to get website ids.How can i get website id or how to assign websites to a product at time of creation

  • rajesh

    Hello Mukesh,

    Great job I would say and awesome contributions in Magento.

    We are trying to add a new category in our magento module sql installer. Do you know how can we achieve the same ?

    Cheers,
    Rajesh

  • http://magento.developmentindia.org magedeveloper

    Hi,

    I want to add new datetime attribute i have used your code but in admin only calendar control display with date only no time field displayed please help how can i display time value for attribute.

    Thanks

  • bhab

    Hi
    I have used your code but nothing is shown in the admin side. should anything else be done other than the steps you mentioned above.I followed your steps and then enabled my module but nothing is displayed.
    Also can this be the issue because of version. I am using Magento 1.4.1.1.

    Thanks

  • Bhabishyat_bikram

    hi
    thanks a lot. i have it working properly now but i have a problem i want the editor enabled for the attribute i added. i tried ‘wysiwyg’ => true, but nothing is shown.
    Can you help me out.

  • Lakshyami

    Hi…

    Is this working for magento 1.4.1.0?

    Thanks.

  • Lakshyami

    Hi…

    Is this working for magento 1.4.1.0?

    Thanks.

  • davidselo

    i try this but don´t work
    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup(‘core_setup’);
    $installer->startSetup();
    $setup->addAttribute(‘catalog_category’, ‘inicio_camp’, array(
    ‘group’ => ‘General’,
    ‘input’ => ‘date’,
    ‘type’ => ‘datetime’,
    ‘label’ => ‘Inicio’,
    ‘backend’ => “eav/entity_attribute_backend_datetime”,
    ‘visible’ => 1,
    ‘required’ => 0,
    ‘user_defined’ => 1,
    ‘global’ => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    ));
    $installer->endSetup();
    why?? i have Magento 1.5

  • Aliasif_bce

    Great Tutorial.
    I need your help . I followed your tutorial every thing is done as explained but when i save category , changes to custom attribute does not change.
    Can you explain why this is happening.
    And one other question how can we create a custom group of attribute instead of ‘General’.
    Thanks
    Regards
    ALI ASIF

  • Aliasif_bce

    Great Tutorial.
    I need your help . I followed your tutorial every thing is done as explained but when i save category , changes to custom attribute does not change.
    Can you explain why this is happening.
    And one other question how can we create a custom group of attribute instead of ‘General’.
    Thanks
    Regards
    ALI ASIF

  • Aliasif_bce

    Great Tutorial.
    I need your help . I followed your tutorial every thing is done as explained but when i save category , changes to custom attribute does not change.
    Can you explain why this is happening.
    And one other question how can we create a custom group of attribute instead of ‘General’.
    Thanks
    Regards
    ALI ASIF

  • Gopalpatelit2010

    $installer = $this;$setup = new Mage_Eav_Model_Entity_Setup(‘core_setup’);$installer->startSetup(); $setup->addAttribute(‘catalog_category’, ‘my_attribute’, array(    ‘group’         => ‘General’,    ‘input’         => ‘select’,    ‘type’          => ‘boolean’,    ‘label’         => ‘My Attribute’,    ‘backend’       => ”,    ‘visible’       => 1,    ‘required’      => 0,    ‘user_defined’ => 1,    ‘global’        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,)); $installer->endSetup();put this code for drop down but i dont know for value yes or no so please give me advice.Thank in advance.

  • Pingback: Añadir atributo a una categoría en MagentoEl Blog de Selo()

  • magentodevel86

    i want to see the xml-file with the setup_mymodule. Can you show me an example please?

  • magentodevel86

    i want to see the xml-file with the setup_mymodule. Can you show me an example please?

  • Vishal Sanwar

    hiii

    It is a great ..

    but  I am facing   a problem,which is ,i want to display these category attribute field on front page
    ,how to solve these ????

    Thanxs

  • Yashgt

    The above code will create a category with the code ‘my_attribute‘ and lable ‘My Attribute‘.

    I think you mean create new attribute and not category

  • Pingback: Adding a category attribute in Magento « magenting()

  • Pingback: Adding a category attribute in Magento « magenting()

  • Parul

    I want to create category multiselect attribute how to ???

  • Vishalsanwar

    HI  Parul
    I have to solved these problem.
    i have to done the direct entry in eav_attribute.

    following are the record of my multiselect field.

    142, 3, ‘show’, NULL, ‘eav/entity_attribute_backend_array’, ‘varchar’, NULL, NULL, ‘multiselect’, ‘Country’, NULL, ”, 0, 0, ‘0’, 0, ”

    Thanx 

    Vishal

  • Vishalsanwar

    HI  ParulI have to solved these problem.i have to done the direct entry in eav_attribute.following are the record of my multiselect field.142, 3, ‘show’, NULL, ‘eav/entity_attribute_backend_array’, ‘varchar’, NULL, NULL, ‘multiselect’, ‘Country’, NULL, ”, 0, 0, ‘0’, 0, ”Thanx Visha

  • Parul

     I have done this entry by setup file ..But problem is that when i make this entry as required field (is_required = 1)It cant save this field…..

  • Pingback: Adding a category attribute in Magento | Catgento()

  • Vishalsanwar

    when u  make this entry as required field ,so u should enter the value of these field.
    Go to  direct in DB and Make it  (is_required = 0) …

  • Pingback: Magento tips « Peter Jaap's Blog()

  • Pingback: Adding a category attribute in Magento | 健忘者的备忘录()

  • 3afsus

    pour activer l’editeur wysiwyg faite un update sql
    $installer->updateAttribute(‘catalog_category’, ‘certifications’, ‘is_wysiwyg_enabled’, 1);

    $installer->updateAttribute(‘catalog_category’, ‘certifications’, ‘is_html_allowed_on_front’, 1);