Magento: Add new tab to Customer Account Section

Suppose, you have a custom Magento module and you wanted to add a custom tab to “Customer My Account” section, i.e. the frontend part displayed after customer login.

Here is the solution:-

Let’s suppose, your module’s name is Yourmodule.

1) Add these xml code in app/design/frontend/your_package/your_theme/layout/yourmodule.xml

Note:- In default Magento settings, your_package = default & your_theme = default, i.e. if you are using the default Magento theme and package then your path will be app/design/frontend/default/default/layout/

yourmodule.xml is the layout file for your module.


<?xml version="1.0"?>
<layout version="0.1.0">
    <customer_account>        
        <reference name="customer_account_navigation">           
	    <action method="addLink" translate="label" module="yourmodule">
	        <name>viewyourmodule</name>
		<path>yourmodule/customer/view</path>
		<label>Yourmodule</label>
	    </action>
	</reference>
    </customer_account>	

    <yourmodule_customer_view>
	<update handle="customer_account"/>
	<reference name="content">
	    <block type="yourmodule/customer" name="view.yourmodule" template="yourmodule/customer/view.phtml"/>
	</reference>
    </yourmodule_customer_view>
</layout>

2) Create controller class file app/code/local/YourNamespace/Yourmodule/controllers/CustomerController.php


class YourNamespace_Yourmodule_CustomerController extends Mage_Core_Controller_Front_Action
{	
    /**
     * Checking if user is logged in or not
     * If not logged in then redirect to customer login
     */
    public function preDispatch()
    {
       	parent::preDispatch();

       	if (!Mage::getSingleton('customer/session')->authenticate($this)) {
            $this->setFlag('', 'no-dispatch', true);
			
	    // adding message in customer login page
	    Mage::getSingleton('core/session')
                ->addSuccess(Mage::helper('yourmodule')->__('Please sign in or create a new account'));
        }
    }			
				
    /**
     * View Your Module
     */
    public function viewAction()
    {					
	$this->loadLayout();        
       	$this->getLayout()->getBlock('head')->setTitle($this->__('Your Module Title'));		
	$this->renderLayout();
    }
}	

3) Create a block class app/code/local/YourNamespace/Yourmodule/Block/Customer.php


<?php
class YourNamespace_YourModule_Block_Customer extends Mage_Core_Block_Template
{
	// YOUR CODE GOES HERE
}

4) Create template file app/design/frontend/your_package/your_theme/template/yourmodule/customer/view.phtml

Add some random text on view.phtml

Now you should be able to see a new tab in ‘Customer My Account’ section.

Hope it helps. Thanks.