when you want to add a custom tab in the Customer information (Magento admin), this article will help you out for adding a custom tab.
3 different methods we have described below which will help you to add a custom tab in Customer information (Magento admin)
Method 1:
Override the file /app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tabs.php,
Inside _beforeToHtml() method, add the following code:
1 2 3 4 5 |
$this->addTab('Custom',array( 'label' =>Mage::helper('customer')->__('Custom'), 'class' => 'ajax', 'url' => $this->getUrl('*/*/custom',array('_current'=>true)), )); |
Override the file /app/code/core/Mage/Adminhtml/controllers/CustomerController.php,
Add the following code:
1 2 3 4 5 6 7 8 9 |
public function customAction() { $this->_initCustomer(); $this->getResponse()->setBody( $this->getLayout()->createBlock('modulename/adminhtml_customer_edit_tab_custom','admin.customer.custom')->setCustomerId(Mage::registry('current_customer')->getId()) ->setUseAjax(true) ->toHtml() ); } |
Create the file /app/code/core/Namespace/ModuleName/Block/Adminhtml/Customer/Edit/Tab/ and create Custom.php,
Add the following source code to the file:
1 2 3 4 5 6 7 8 |
class Namespace_ModuleName_Block_Adminhtml_Customer_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Form { public function __construct() { parent::__construct(); $this->setTemplate('modulename/customer/tab/custom.phtml'); } } |
Now, you need to create a template file.
Go to /app/design/adminhtml/default/default/template/modulename/customer/tab/ and create custom.phtml,
Write your code here.
Method 2:
Add this code to your custom module config.xml located at Namespace/ModuleName/etc/config.xml,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<config> <global> <events> <core_block_abstract_prepare_layout_after> <observers> <namespace_modulename_observer> <type>singleton</type> <class>Namespace_ModuleName_Model_Observer</class> <method>injectTabs</method> </namespace_modulename_observer> </observers> </core_block_abstract_prepare_layout_after> </events> </global> </config> |
Now create one Observer at Namespace/ModuleName/Model/Observer.php,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Namespace_ModuleName_Model_Observer { public function __construct() { } public function injectTabs($observer) { $block = $observer->getEvent()->getBlock(); // add tab in customer edit page if ($block instanceof Mage_Adminhtml_Block_Customer_Edit_Tabs) { if ($this->_getRequest()->getActionName() == 'edit' || $this->_getRequest()->getParam('type')) { $block->addTab('domains', array('label'=> Mage::helper('customer')->__('Manage Licensed Domains'),'url'=> $block->getUrl('*/*/custom', array('_current' => true)),'class'=> 'ajax')); } } } protected function _getRequest() { return Mage::app()->getRequest(); } } |
Override the file /app/code/core/Mage/Adminhtml/controllers/CustomerController.php,
Add the following code:
1 2 3 4 5 6 7 8 |
public function customAction(){ $this->_initCustomer(); $this->getResponse()->setBody( $this->getLayout()->createBlock('modulename/adminhtml_customer_edit_tab_custom','admin.customer.custom')->setCustomerId(Mage::registry('current_customer')->getId()) ->setUseAjax(true) ->toHtml() ); } |
Create the file /app/code/core/Namespace/ModuleName/Block/Adminhtml/Customer/Edit/Tab/ and create Custom.php,
Add the following source code to the file:
1 2 3 4 5 6 7 8 |
class Namespace_ModuleName_Block_Adminhtml_Customer_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Form { public function __construct() { parent::__construct(); $this->setTemplate('modulename/customer/tab/custom.phtml'); } } |
Now, you need to create a template file.
Go to /app/design/adminhtml/default/default/template/modulename/customer/tab/ and create custom.phtml,
Write your code here.
Method 3:
Go to app/code/local/Namespace/Modulename/etc/config.xml,
1 2 3 4 5 6 7 8 9 10 11 |
<config> <adminhtml> <layout> <updates> <modulename> <file>modulename.xml</file> </modulename> </updates> </layout> </adminhtml> </config> |
Go to app/design/adminhtml/default/default/layout/modulename.xml,
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <layout> <adminhtml_customer_edit> <reference name="customer_edit_tabs"> <action method="addTab"> <name>my_custom_tab</name> <block>modulename/adminhtml_customer_tab</block> </action> </reference> </adminhtml_customer_edit> </layout> |
Go to app/code/local/Namespace/ModuleName/Block/Adminhtml/Customer/Tab.php,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php class Namespace_ModuleName_Block_Adminhtml_Customer_Tab extends Mage_Adminhtml_Block_Template implements Mage_Adminhtml_Block_Widget_Tab_Interface { /** * Set the template for the block * */ public function _construct() { parent::_construct(); $this->setTemplate('modulename/customer/tab.phtml'); } /** * Retrieve the label used for the tab relating to this block * * @return string */ public function getTabLabel() { return $this->__('My Custom Tab'); } /** * Retrieve the title used by this tab * * @return string */ public function getTabTitle() { return $this->__('Click here to view your custom tab content'); } /** * Determines whether to display the tab * Add logic here to decide whether you want the tab to display * * @return bool */ public function canShowTab() { return true; } /** * Stops the tab being hidden * * @return bool */ public function isHidden() { return false; } } |
Go to app/design/adminhtml/default/default/template/modulename/customer/tab.phtml,
1 2 3 4 5 6 7 8 9 |
<?php /** * Custom tab template */ ?> <div class="input-field"> <label for="custom_field">Custom Field</label> <input type="text" class="input-text" name="custom_field" id="custom_field" /> </div> |
————————————————————————————————————————————————————————————————————–
Result :
Issue Fixed By: Nimit Shah
Guided By: Sunil Patel
hello sir
when i use method 2 getting error:
Fatal error: Call to a member function setCustomerId() on a non-object in D:\Xampp_new\xampp\htdocs\magento18\app\code\local\Package\Customtabs\controllers\CustomerController.php on line 9
Please advice