Magento – add custom tab to customer adminhtml screen

adminhtml

I know this has been asked so many times before however I more so have a question.

I am copying the file

/app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tabs.php

and putting it in

/app/code/local/Mage/Adminhtml/Block/Customer/Edit/Tabs.php

I then edit the Tabs.php to have a new tab however when I refresh my screen, I even logged out cleared my cache and logged back in Magento does not seem to read from the app/code/local/mage directory?

However if I make the same changes in the file located in the core folder my changes work? So why can I not more the file to the local file?

I am using Magento CE 1.9.1.0
Shouldn't I be able to copy any file into the local directory and as long as the folder structure is the same it should load it??

Best Answer

I think none of the other answers (at this time) does it 'the Magento way'. To know how to do it the Magento way, have a look at Magento's Billing Agreements. It adds a tab to the customer edit page as well.

First, app/design/adminhtml/default/default/layout/sales.xml:

<adminhtml_customer_edit>
    <reference name="customer_edit_tabs">
        <action method="addTab"><name>customer_edit_tab_agreements</name><block>sales/adminhtml_customer_edit_tab_agreement</block></action>
        ....
    </reference>
</adminhtml_customer_edit>

Now you wonder: I have no title or content! No problem. Have a look at the above referenced block Mage_Sales_Block_Adminhtml_Customer_Edit_Tab_Agreement (in app/code/core/Mage/Sales/Block/Adminhtml/Customer/Edit/Tab/Agreement.php). I don't think it's useful to copy/paste the entire file here, but all the magic is there, like getTabLabel(), getTabTitle() and canShowTab(). It extends Mage_Sales_Block_Adminhtml_Billing_Agreement_Grid because there's a grid in it, but you could also extend the more general Mage_Adminhtml_Block_Widget.

You didn't mention the use of your tab, but you can even create a whole layout and then reference it by name:

<adminhtml_customer_edit>
    <reference name="customer_edit_tabs">
        <block type="my_module/adminhtml_customer_edit_tab_nameoftab" name="mytab">
            ... add some blocks here ...
        </block>
        <action method="addTab"><name>mytab</name><block>mytab</block></action>
    </reference>
</adminhtml_customer_edit>

So here's what you need to do:

  • create your own module (you've probably already done that)
  • create a layout file in the adminhtml area to add the tab, specifying the block class
  • create the block in your module and create the functions required by the interface (Mage_Adminhtml_Block_Widget_Tab_Interface)

No need for event observers, no need for local overrides, no need for editing the core!

Related Topic