Engine23
blogBannerImage
By Engine23

Magento - How to create custom Customer Tab and submit a form

If you ever needed to create a custom Customer tab and put some information there, as well as submit a form, this should give you enough information to accomplish this task.

Create Module declaration

app/etc/modules/Russellalbin_Customertab.xml

<?xml version="1.0"?>
<config>
<modules>
<Russellalbin_Customertab>
<active>true</active>
<codePool>local</codePool>
</Russellalbin_Customertab>
</modules>
</config>

Create the module configuration file

app/etc/local/Russellalbin/Customertab/etc/config.xml

<config>
<modules>
<Russellalbin_Customertab>
<version>0.0.1</version>
</Russellalbin_Customertab>
</modules>
<adminhtml>
<layout>
<updates>
<customertab>
<file>customertab.xml</file>
</customertab>
</updates>
</layout>
</adminhtml>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<russellalbin_customertab before="Mage_Adminhtml">Russellalbin_Customertab_Adminhtml</russellalbin_customertab>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<global>
<blocks>
<customertab>
<class>Russellalbin_Customertab_Block</class>
</customertab>
</blocks>
</global>
</config>

Create the Block for this new Customertab

app/code/local/Russellalbin/Customertab/Block/Adminhtml/Customer/Edit/Tab/Action.php

<?php
/**
* Adminhtml customer action tab
*
*/
class Russellalbin_Customertab_Block_Adminhtml_Customer_Edit_Tab_Action
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
public function __construct()
{
$this->setTemplate('customertab/action.phtml'); }
public function getCustomtabInfo(){
$customer = Mage::registry('current_customer');
$customtab='Mail Order Comics Pull List';
return $customtab;
}
/**
* Return Tab label
*
* @return string
*/
public function getTabLabel()
{
return $this->__('Customer Pull List');
}
/**
* Return Tab title
*
* @return string
*/
public function getTabTitle()
{
return $this->__('Pull List Tab');
}
/**
* Can show tab in tabs
*
* @return boolean
*/
public function canShowTab()
{
$customer = Mage::registry('current_customer');
return (bool)$customer->getId();
}
/**
* Tab is hidden
*
* @return boolean
*/
public function isHidden()
{
return false;
}
/**
* Defines after which tab, this tab should be rendered
*
* @return string
*/
public function getAfter()
{
return 'tags';
}
}
?>

 

Create the controller to be used when we post the form from our new tab/page

app/code/local/Russellalbin/Customertab/controllers/Adminhtml/CustomertabController.php

<?php
class Russellalbin_Customertab_Adminhtml_CustomertabController extends Mage_Adminhtml_Controller_Action
{
function resetAction()
{
$params = array();
$path = 'customer';
$customer_id = (int)$this->getRequest()->getParam('customer_id');
if($customer_id)
{
// Do your stuff here

$params['id'] = $customer_id;
$params['back'] = 'edit';
$params['tab'] = 'customer_edit_tab_action';
$path = 'adminhtml/customer/edit/';
}
$params['_store'] = Mage::getModel('core/store')->load(0);
$url = Mage::getModel('adminhtml/url')->getUrl($path, $params);
Mage::app()
->getResponse()
->setRedirect($url);
Mage::app()
->getResponse()
->sendResponse();
exit;
}
}

Create the Adminhtml design xml

app/design/adminhtml/default/default/layout/customertab.xml

<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_customer_edit>
<reference name="customer_edit_tabs">
<action method="addTab"><name>customer_edit_tab_action</name><block>customertab/adminhtml_customer_edit_tab_action</block></action>
</reference>
</adminhtml_customer_edit>
</layout>

Create the phtml file for our new content and form

app/design/adminhtml/default/default/template/customertab/action.phtml

<div id="customer_info_tabs_customer_edit_tab_action_content">
<div class="entry-edit">
<div class="entry-edit-head">
<h4 class="icon-head head-edit-form fieldset-legend">Pull List</h4>
</div>
<div id="group_fields4" class="fieldset fieldset-wide">
<div class="hor-scroll">
<h2>This is my form and content that I can submit and return back to this page and tab</h2>
<div><form action="&lt;?php echo $this-&gt;getUrl('adminhtml/customertab/reset/', array('customer_id'=&gt;$customer_id)); ?&gt;" method="get"><input type="submit" value="Post This Form" /></form></div>
</div>
</div>
</div>
</div>