atwix

Adding a new tab to the product edit page in admin

There are 2 ways to add a new tab to the product edit page in admin panel of Magento.

First method:
Let’s add a tab for a custom module.
Add the following code to your config.xml file in the global scope:

<blocks>
...
    <modulename>
        <class>Company_ModuleName_Block</class>
    </modulename>
    <adminhtml>
        <rewrite>
             <catalog_product_edit_tabs>Company_ModuleName_Block_Adminhtml_Tabs</catalog_product_edit_tabs>
         </rewrite>
     </adminhtml>
...
</blocks>

After this you should create a new file: Company/ModuleName/Block/Adminhtml/Tabs.php The content of this file you can see below

<?php

class Company_ModuleName_Block_Adminhtml_Tabs extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs
{
    private $parent;

    protected function _prepareLayout()
    {
        //get all existing tabs
        $this->parent = parent::_prepareLayout();
        //add new tab
        $this->addTab('tabid', array(
                     'label'     => Mage::helper('catalog')->__('New Tab'),
                     'content'   => $this->getLayout()
             ->createBlock('modulename/adminhtml_tabs_tabid')->toHtml(),
        ));
        return $this->parent;
    }
}

The code above rewrites the system function which generates the layout and adds the tab.

Next, you need to create a file: Company/ModuleName/Block/Adminhtml/Tabs/Tabid.php

<?php

class Company_ModuleName_Block_Adminhtml_Tabs_Tabid extends Mage_Adminhtml_Block_Widget
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('modulename/newtab.phtml');
    }
}

Note: template should be in app/design/adminhtml/…/…/template/

Second method:

Simply copy the file app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tabs.php to a local folder (app/code/local/Mage/Adminhtml/Block/Catalog/Product/Edit/Tabs.php) and add the following snippet to the function _prepareLayout()

$this->addTab('tabid', array(
              'label'     => Mage::helper('catalog')->__('New Tab'),
              'content'   => $this->_translateHtml($this->getLayout()
     ->createBlock('modulname/adminhtml_tabs_tabid')->toHtml()),
));

After this, simply follow the instructions above.

We hope this article was helpful. Comments are welcome.


  • Ivan

    how about adding tab to your created module?..

  • Fischerwfc

    do i need to add something in order for the data to save? like do i need to create a new column or table? where does data like this get saved by default?

    • Anonymous

      Hello, thanks for your comment. I just described how to add a new tab without fields. Sure, if you want to add a new field to this tab you need to find some way for saving value, for example: it can be custom table, etc…

  • http://www.facebook.com/ana.rodrigues.963 Ana Rodrigues

    Hi could you help me?

    Is showing the following error: Fatal error: Call to a member function toHtml() on a non-object in D:xampphtdocsMagentoappcodelocalLearningMageBlockAdminhtmlTabs.php on line 14

    Details:
    D:xampphtdocsMagentoappcodelocalLearningMageBlockAdminhtmlTabs.php
    parent = parent::_prepareLayout();
    //add new tab
    $this->addTab(‘tabid’, array(
    ‘label’ => Mage::helper(‘catalog’)->__(‘New Tab’),
    ‘content’ => $this->_translateHtml($this->getLayout()
    ->createBlock(‘mage/adminhtml_tabs_tabid’)->toHtml()),
    ));
    return $this->parent;
    }
    }

    D:xampphtdocsMagentoappcodelocalLearningMageBlockAdminhtmlTabsTabid.php

    setTemplate(‘mage/newtab.phtml’);
    }
    }

    D:xampphtdocsMagentoappdesignadminhtmldefaultdefaulttemplatenewtab.phtml

    Custom Field

    config.xml
    [...]

    Learning_Mage_Block_Adminhtml_Tabs

    Magento version: 1.7.0.2
    Tahnks

  • John

    Starmall_Producttab_Adminhtml_Tabs

    I put this in /etc/config.xml

    This gives me an ‘Invalid Block type’ in the exception.log.

    Can you help?

  • John

    Sorry, my mistake, it’s working. Had a typo.

  • http://www.facebook.com/rasa7777 Ross Smith

    This didn’t work until I added

    Company_ModuleName_Block

    to inside *and* removed the
    $this->_translateHtml()
    function, which appears to not exist anywhere in magento.

    Why write an imcomplete/buggy tutorial? It just waste’s people time.

    • NickKravchuk

      When you rewrite any block you should declare your own block in config.xml of course, the function _translateHtml is located in block Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs that was rewrote, could you provide version of magento you use? Thanks.

      • http://www.facebook.com/rasa7777 Ross Smith

        Enterprise 1.9.1.1

        • NickKravchuk

          Hello Ross, you are right, this magento version hasn’t the _translateHtml function, good point, thanks :)

        • NickKravchuk

          I’ve updated article a bit, should work for any versions, thanks for sharing your knowledge :)