PHP Magento Tips

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"?>
<config>
    <modules>
        <Mycompany_CustomerInvoices>
            <active>true</active>
            <codePool>local</codePool>
        </Mycompany_CustomerInvoices>
    </modules>
</config>

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"?>
<config>
    <modules>
        <Mycompany_CustomerInvoices>
            <version>1.0.0</version>
        </Mycompany_CustomerInvoices>
    </modules>

    <frontend>
        <routers>
            <customerinvoices>
                <use>standard</use>
                <args>
                    <module>Mycompany_CustomerInvoices</module>
                    <frontName>getinvoices</frontName>
                </args>
            </customerinvoices>
        </routers>

        <layout>
            <updates>
                <getinvoices>
                    <file>mycustomerinvoices.xml</file>
                </getinvoices>
            </updates>
        </layout>
    </frontend>

    <global>
        <blocks>
            <invoices>
                <class>Mycompany_CustomerInvoices_Block</class>
            </invoices>
        </blocks>
    </global>
</config>

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">

 <customer_account>
        <reference name="customer_account_navigation">
            <action method="addLink" translate="label">
                <name>customerinvoices</name>
                <path>getinvoices/customerInvoice</path>
                <label>My Invoices</label>
            </action>
        </reference>
    </customer_account>

    <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"/>
        </reference>
    </customerinvoices_customerinvoice_index>

</layout>

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:

<?php
class Mycompany_CustomerInvoices_CustomerInvoiceController extends Mage_Core_Controller_Front_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->getLayout()->getBlock("head")->setTitle($this->__("Invoices"));
        $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")
        ));
        $this->renderLayout();
    }


    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')
                ->setOrderFilter($order->getId())
                ->load();
            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(),
                    'application/pdf'
                );

            }
        }
    }

    public function preDispatch()
    {
        parent::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:

<?php

class Mycompany_CustomerInvoices_Block_Index extends Mage_Core_Block_Template
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('customer/account/myinvoices.phtml');

        $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();
        $invoices->addFieldToFilter('customer_id',$customerId);
        return $invoices;
    }

    protected function _prepareLayout()
    {
        parent::_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);
        $this->getInvoices()->load();

        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>

 <div class="toolbar"><?php echo $this->getPagerHtml(); ?></div>
 <table id="invoices-table" class="data-table">
  <thead>
   <tr>
    <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>
   </tr>
  </thead>
 <?php
  foreach( $this->getInvoices() as $invoice): ?>
   <tr>
    <td>
     <?php echo $invoice->getIncrementId(); ?>
    </td>
    <td>
     <?php echo $invoice->getCreatedAt(); ?>
    </td>
    <td>
     <a href="<?php echo $this->getUrl('sales/order/view', array('order_id' => $invoice->getOrder()->getId())); ?>">
      <?php echo $invoice->getOrder()->getRealOrderId(); ?>
     </a>
    </td>
    <td>
     <?php echo $invoice->getOrder()->getCreatedAt(); ?>
    </td>
    <td>
     <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'); ?>
     </a>
     &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'); ?>
     </a>
    </td>
   </tr>
  <?php endforeach;
 ?>
 </table>
 <div class="toolbar toolbar-bottom"><?php echo $this->getPagerHtml(); ?></div>
 <?php else: ?>
  <p class="notice-msg"><?php echo $this->__('No invoices'); ?></p>
 <?php endif; ?>
</div>

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: https://github.com/vijayrami/my_invoices
0 Comments On "How to Add Customer Invoices link to My Account Navigation in Magento"

Back To Top