How to add category tree tab to custom module

As you can see, when you try to add or edit a product, you can assign a product to 1 or more category with “Categories” tab.


In my recent project, I had to make a custom module and add categories tab for FAQ. In this tutorial, I will show you how to add a category tab to edit form of a custom module.
Now, let’s create a custom module called “Categorytab”.

Step 1: Add new tab to edit form
First, we need to add a tab name “Categories” to edit form.
This code is written in the class “Magebuzz_Categorytab_Block_Adminhtml_Categorytab_Edit_Tabs” inside _beforeToHtml() function

class Magebuzz_Categorytab_Block_Adminhtml_Categorytab_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
  public function __construct() {
      $this->setTitle(Mage::helper('categorytab')->__('Item Information'));
  protected function _beforeToHtml() {
      $this->addTab('form_section', array(
          'label'     => Mage::helper('categorytab')->__('Item Information'),
          'title'     => Mage::helper('categorytab')->__('Item Information'),
          'content'   => $this->getLayout()->createBlock('categorytab/adminhtml_categorytab_edit_tab_main')->toHtml(),
      $this->addTab('categories', array(
          'label'     => Mage::helper('catalog')->__('Categories'),
          'url'       => $this->getUrl('*/*/categories', array('_current' => true)),
          'class'     => 'ajax',
      return parent::_beforeToHtml();

Step 2: Add an action for admin controller
When you click on Categories Tab, it will call the action in our controller using ajax. And we will write that action in our controller (Magebuzz_Categorytab_Adminhtml_CategorytabController)

public function categoriesAction() {	
  $categorytabId = $this->getRequest()->getParam('id');
  $categorytab = Mage::getModel('categorytab/categorytab')->load($categorytabId);
  Mage::register('categorytab_data', $categorytab);

After that, we need to add the following code to load layout and render the content in adminhtml layout file.

    <block type="core/text_list" name="root" output="toHtml">
       <block type="categorytab/adminhtml_categorytab_edit_tab_categories" name=""/>

The content of tab will be generated after the ajax request sent. Back to the controller file, we write an action name categoriesJson to send a response for Ajax request.

public function categoriesJsonAction() {
  $categorytabId = $this->getRequest()->getParam('id');
  $categorytab = Mage::getModel('categorytab/categorytab')->load($categorytabId);
  Mage::register('categorytab_data', $categorytab);  $this->getResponse()->setBody($this->getLayout()->createBlock('categorytab/adminhtml_categorytab_edit_tab_categories')->getCategoryChildrenJson($this->getRequest()->getParam('category')));

Step 3: Block for the content of categories tab
Now, we will create a block to add content for categories tab in step 2.
Our block (Magebuzz_Categorytab_Block_Adminhtml_Categorytab_Edit_Tab_Categories) is extended from Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Categories. And we need to customize some functions. In this file, you need to add the following code:

protected $_categoryIds;
protected $_selectedNodes = null;
public function __construct() {
protected function getCategoryIds() {
     return $this->getCategorytab()->getCategoryIds();

public function isReadonly() {
     return false;

public function getIdsString() {
     return implode(',', $this->getCategoryIds());

public function getCategorytab() {
     return Mage::registry('categorytab_data');

1. __construct() function to set a template file for the content
2. getCategoryIds() function to get all category ids will be assigned for the current item.

Step 4: Copy the template file
This is the final step, we need to create a template file called categories.phtml. You can copy this file from magento adminhtml core template in the directory app/design/adminhtml/default/default/template/catalog/product/edit and place in app/design/adminhtml/default/default/template/categorytab/

6 thoughts on “How to add category tree tab to custom module”

6 Responses to How to add category tree tab to custom module

  • aisha
    aisha on March 25, 2014 at 1:51 am said:

    Hello. I'm getting this error. Call to undefined function getCategoryIds() in <b>F:\ext\localinstance\app\code\local\ -- \Categorytab\Block\Adminhtml\Categorytab\Edit\Tab\Categories.php
    can u please help out.

  • guigui2mars
    guigui2mars on April 22, 2015 at 2:21 am said:

    If you get error about the implode
    Modify this function by this

    protected function getCategoryIds() {
    return array();
    return $this->getCategorytab()->getCategoryIds();

  • Bhavesh
    Bhavesh on April 24, 2015 at 7:29 pm said:

    Use full your code for category tree in custom module for me but i have add item and select category then save but not save in category.

    Can you help me???

    Bhavesh Patel

  • usha
    usha on May 15, 2015 at 2:56 am said:

    Hi, I tried ur code but its not working properly for me , can u send me the full source code to include Category Tree structure into my custom module like leftside tab.

  • Nilesh
    Nilesh on June 8, 2015 at 8:54 pm said:

    I'm also getting same error.

  • Athul
    Athul on April 27, 2016 at 8:49 pm said:

    I am not getting the output. It is showing a duplicate screen of the admin panel