PHP Magento Tips

Powered by Blogger.

How to Add Customer Invoices link to My Account Navigation in Magento

Rate this posting:

To add Customer Invoices link to My Account Navigation in Magento Frontend Just follow below steps:

Step 1: Create app/etc/modules/Mycompany_CustomerInvoices.xml file and add below code in it:

<?xml version="1.0"?>

Step 2: Create config.xml file. create app/code/local/Mycompany/CustomerInvoices/etc/config.xml file and add below code in it:

<?xml version="1.0"?>




Here we create our front name as "getinvoices", which is useful to generate url in frontend (which will be used later) and our layout file and set block folder path.

Step 3: Now create your layout.xml file inside your theme folder. Create app/design/frontend/yourtheme/default/layout/mycustomerinvoices.xml file and add below code in it.

<?xml version="1.0"?>
<layout version="0.1.0">

        <reference name="customer_account_navigation">
            <action method="addLink" translate="label">
                <label>My Invoices</label>

    <customerinvoices_customerinvoice_index translate="label">
        <label>Customer Invoices</label>
        <update handle="customer_account"/>

        <reference name="my.account.wrapper">
            <block type="invoices/index" name="customer.invoices" template="customer/account/myinvoices.phtml"/>


Here we create update handle as customer_account and in that we add new link with addLink method. In that we set a path as getinvoices/customerInvoice which will be your frontname node / controllername. if you don't pass any action here then it will take index action.

Here we create customerinvoices_customerinvoice_index (yourmodule_controllername_actionname) node to set layout for our new generated page.

Also we set a Block as invoices/index which follow as your node name below <blocks> node in config.xml and in that block folder called php file that you want to call. Here it will called index.php file inside block folder. And last we set template file.

Step 4: Create Controller file. create app/code/local/Mycompany/CustomerInvoices/controllers/CustomerInvoiceController.php file and add below code in it:

class Mycompany_CustomerInvoices_CustomerInvoiceController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        $breadcrumbs = $this->getLayout()->getBlock("breadcrumbs");
        $breadcrumbs->addCrumb("home", array(
            "label" => $this->__("Home Page"),
            "title" => $this->__("Home Page"),
            "link"  => Mage::getBaseUrl()

        $breadcrumbs->addCrumb("customer-invoices", array(
            "label" => $this->__("Invoices"),
            "title" => $this->__("Invoices")

    public function viewAction() {
        $orderId = (int) $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($orderId);

        if ($this->_canViewOrder($order)) {
            $invoices = Mage::getResourceModel('sales/order_invoice_collection')
            if ($invoices->getSize() > 0) {
                $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);

                return $this->_prepareDownloadResponse(
                    'invoice-'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),


    public function preDispatch()
        $action = $this->getRequest()->getActionName();
        $loginUrl = Mage::helper('customer')->getLoginUrl();

        if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);

    protected function _canViewOrder($order)
        $customerId = Mage::getSingleton('customer/session')->getCustomerId();
        $availableStates = Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates();
        if ($order->getId() && $order->getCustomerId() && ($order->getCustomerId() == $customerId)
            && in_array($order->getState(), $availableStates, $strict = true)
        ) {
            return true;
        return false;

In this controller file first called preDispatch action. which will check customer authentication.

we can check for login url and if it is not found then with $this->setFlag('', self::FLAG_NO_DISPATCH, true); we can stop controller displatch.

In indexAction we set breadcrumb between loadLayout and renderLayout method.

viewAction is responsible to generate PDF file of invoice.

PDF is generated with $this->_prepareDownloadResponse function.

We can also generate CSV for any other application also. Just follow below syntax:

$data = 'your csv data';

$this->_prepareDownloadResponse('file.csv', $data, 'text/csv');

Here _canViewOrder function check for all order conditions and then allow to generate a PDF.

Step 5: create block index file. Create app/code/local/Mycompany/CustomerInvoices/Block/Index.php file and add below code in it:


class Mycompany_CustomerInvoices_Block_Index extends Mage_Core_Block_Template
    public function __construct()

        $this->setInvoices( $this->getAllInvoices() );

    public function getAllInvoices()
        $invoices = Mage::getResourceModel('sales/order_invoice_collection');
        $select = $invoices->getSelect();
        $select->joinLeft(array('order' => Mage::getModel('core/resource')->getTableName('sales/order')), 'order.entity_id=main_table.order_id', array('customer_id' => 'customer_id'));
        $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
        return $invoices;

    protected function _prepareLayout()

        $pager = $this->getLayout()->createBlock('page/html_pager', 'invoices.pager');
        $pager->setAvailableLimit(array(10=>10, 15=>15, 30=>30, $this->getInvoices()->getSize() => $this->__('All') ));
        $pager->setCollection( $this->getInvoices() );

        $this->setChild('pager', $pager);

        return $this;

    public function getPagerHtml()
        return $this->getChildHtml('pager');

Here we add Pagination in _prepareLayout function.and in front end you can add pagination with

<?php echo $this->getPagerHtml(); ?>

Step 6: Create your template file inside your theme folder. create app/design/frontend/vijay/default/template/customer/account/myinvoices.phtml file and add below code in it.

<div id="customer-invoices">
 <?php if( $this->getInvoices()->getSize() ): ?>

 <div class="page-title">
  <h1><?php echo $this->__('My Invoices'); ?></h1>

 <div class="toolbar"><?php echo $this->getPagerHtml(); ?></div>
 <table id="invoices-table" class="data-table">
    <th><strong><?php echo $this->__('Invoice Number'); ?></strong></th>
    <th><strong><?php echo $this->__('Invoice Date'); ?></strong></th>
    <th><?php echo $this->__('Order #'); ?></th>
    <th><?php echo $this->__('Order Date'); ?></th>
    <th width="15%"><?php echo $this->__('Actions'); ?></th>
  foreach( $this->getInvoices() as $invoice): ?>
     <?php echo $invoice->getIncrementId(); ?>
     <?php echo $invoice->getCreatedAt(); ?>
     <a href="<?php echo $this->getUrl('sales/order/view', array('order_id' => $invoice->getOrder()->getId())); ?>">
      <?php echo $invoice->getOrder()->getRealOrderId(); ?>
     <?php echo $invoice->getOrder()->getCreatedAt(); ?>
     <a href="<?php echo $this->getUrl('getinvoices/customerInvoice/view',
       array('order_id' => $invoice->getOrder()->getId())); ?>">
      <i class="fa fa-file-pdf-o"></i>
      <?php echo $this->__('Create PDF'); ?>
     &nbsp; | &nbsp;
     <a href="<?php echo $this->getUrl('sales/order/invoice', array('order_id' => $invoice->getOrder()->getId()))
      <i class="fa fa-eye"></i>
      <?php echo $this->__('View'); ?>
  <?php endforeach;
 <div class="toolbar toolbar-bottom"><?php echo $this->getPagerHtml(); ?></div>
 <?php else: ?>
  <p class="notice-msg"><?php echo $this->__('No invoices'); ?></p>
 <?php endif; ?>

To generate PDF url we give a view action.with order_id parameter as below:

<?php echo $this->getUrl('mycompany/customerInvoice/view',
                             array('order_id' => $invoice->getOrder()->getId())); ?>

which you can check at front end.

Now login to your account, you can find "My Invoices" link in your account navigation dashboard.

Download This Module on Github:
0 Comments On "How to Add Customer Invoices link to My Account Navigation in Magento"

Back To Top