Magento order invoice pdf in customer account dashboard
Magento order invoice pdf in customer account dashboard

Would you like to fetch magento admin panel feature in front-end.So here is your solution.Now I am going to show you how to fetch Magento order invoice pdf in customer account dashboard.Simply it is a admin panel feature that can only used in sales section of admin panel but here you will getting that feature in front-end.In customer account dashboard instead of view order link you can set view order PDF link which will open invoice pdf of respected order in dashboard of customer account.Generally some clients wants such type of requirement and it is not magento default feature, I am writing this article for giving you one of solution.I have done this solution in my project and I would like to make this solution as public so that other can get benefit on this.Lets come to our topic that how can we implement this.Lets see I am doing one by one.Here I am giving you the full structure of a module because its a simple module.It has taken just two main one is controller and other is config .So lets see what these files contents.

First creating a module you have declare your module in magento. Also I am not giving full details how we can built magento custom module.I am just giving you how simply I have done.
For reference: I have created a new module called JRB_Frontendorderpdf.
As we need a controller for that we have defined the controller rout in our config.xml file , so that we define our custom URL to point out our controller where we can generate PDF. Lets see how I have declare in config.xml file.

Configuration:

Dir: app/code/[CodePool]/[NameSpace]/[ModuleName]/etc/config.xml
e.g: app/code/local/JRB/Frontendorderpdf/etc/config.xml

<?xml version="1.0"?>
<!--
/*
* category JRB
* module JRB_Frontendorderpdf
* 
* author Jyotiranjan Biswal(biswal@jyotiranjan.in)
*/
-->
<config>
  <modules>
    <jrb_frontendorderpdf>
      <version>0.1.0</version>
    </jrb_frontendorderpdf>
  </modules>
  <frontend>
    <routers>
      <frontendorderpdf>
        <use>standard</use>
          <args>
            <module>JRB_Frontendorderpdf</module>
            <frontname>frontendorderpdf</frontname>
          </args>
      </frontendorderpdf>
    </routers>
  </frontend>
</config>

Now I have declare my controller name as ‘frontendorderpdf’ and I can access my controller function through this name.Here I am just explaing a little bit how controller works in magento.
For example: http://localhost/magento/index.php/frontendorderpdf/pdf/invoicespdf/order_id/2/
This URL can be divided in different part like

  • baseUrl: http://localhost/magento/index.php/
    • it may differ on basis of website URL.you can get by $this->getUrl() or $this->getBaseUrl().
  • frontendName: frontendorderpdf
    • So if we type this name after base URL in browser URL then it we will point to our module controller section .
  • controllerName: pdf
    • controllerName defines which controller class will be called in module.Here we have controllerName called pdf which points to our PdfController class in dir : app/code/local/JRB/Frontendorderpdf/controllers/PdfController.php .Bydefault controllerName can be defined as IndexController.php
  • actionName: invoicespdf
    • actionName defines the function where we implement our logical part.
  • params: order_id/2
    • params are the parameter that used to pass through URL.

So lets see our controller class and action with our custom logic that how we have implements.In same way you can implement your own logic .But here for generating pdf of order invoice ,I have used some logic and model class by using order id.

The Controller:

Dir: app/code/[CodePool]/[NameSpace]/[ModuleName]/controllers/controllerclass
e.g: app/code/local/JRB/Frontendorderpdf/controllers/PdfController.php

<?php
/*
* category JRB
* module JRB_Frontendorderpdf
* 
* author Jyotiranjan Biswal(biswal@jyotiranjan.in)
*/
class JRB_Frontendorderpdf_PdfController extends Mage_Core_Controller_Front_Action{
    /*
    * category JRB
    * module JRB_Frontendorderpdf
    * create invoice by order ID
    * 
    * author Jyotiranjan Biswal(biswal@jyotiranjan.in)
    */
    public function invoicespdfAction() {
        $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($orderId)
                ->load();
            if ($invoices->getSize() > 0) {
                $flag = true;
                if (!isset($pdf)){
                    $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                } else {
                    $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                    $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                }
            }
	    if ($flag) {
		return $this->_prepareDownloadResponse(
		    'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
		    'application/pdf'
		);
	    } else {
		Mage::getSingleton('core/session')->addError($this->__('There are no printable documents related to selected orders.'));
		$this->_redirectReferer();
	    }
        }
    }
    /*
    * category JRB
    * module JRB_Frontendorderpdf
    * check for order visibility
    * 
    * author Jyotiranjan Biswal(biswal@jyotiranjan.in)
    */
    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;
    }
}
?>

Here I have used Mage_Sales_Model_Order_Pdf_Invoice model class by Mage::getModel(‘sales/order_pdf_invoice’) and called the function getPdf() by getPdf($invoices).So my finale code for accessing pdf is : Mage::getModel(‘sales/order_pdf_invoice’)->getPdf($invoices);

Upto to you are able to know how to create pdf with our custom moudle.But our main part is where and how to implement this logic so that we can generate pdf in dashboard of customer account.Lets see where we can set our controller URL to generate PDF.if you will go to dir: app/design/frontend/base/default/template/sales/order then you will be getting two files one is recent.phtml and other is history.phtml.

You can copy those file to your theme directory to override the core file and you can edit without fear. Because you will be safe at the time of up-gradation. Or if you theme has overrided already then edit direct in that folder by taking backup.

history.phtml:

dir: app/design/frontend/base/default/template/sales/order/history.phtml
changed to app/design/frontend/default/default/template/sales/order/history.phtml
or changed to app/design/frontend/themefolder/themefolder/template/sales/order/history.phtml

then go the line number 62(near to) and search for

<a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order') ?></a>

original:

<a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order') ?></a>

changed to :

<?php if($_order->hasInvoices() || $_order->getBaseTotalDue() == 0):?>
    <a href="<?php echo $this->getUrl('frontendorderpdf/pdf/invoicespdf', array('order_id' => $_order->getId())) ?>"><?php echo $this->__('View Order PDF') ?></a>
<?php else:?>
    <a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order') ?></a>
<?php endif;?>

recent.phtml:

dir: app/design/frontend/base/default/template/sales/order/history.phtml
changed to app/design/frontend/default/default/template/sales/order/history.phtml
or changed to app/design/frontend/themefolder/themefolder/template/sales/order/history.phtml

then go the line number 62(near to) and search for

<a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order') ?></a>

original:

<a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order') ?></a>

changed to :

<?php if($_order->hasInvoices() || $_order->getBaseTotalDue() == 0):?>
    <a href="<?php echo $this->getUrl('frontendorderpdf/pdf/invoicespdf', array('order_id' => $_order->getId())) ?>"><?php echo $this->__('View Order PDF') ?></a>
<?php else:?>
    <a href="<?php echo $this->getViewUrl($_order) ?>"><?php echo $this->__('View Order') ?></a>
<?php endif;?>

Or you can find full module here Order Invoice In Dashboard Frontend.Make sure you have taken backup of your recent.phtml and history.phtml file before installing this into your project.

Hope you have got your solution through the Magento order invoice pdf in customer account dashboard and never forget to share to help others and never hesitate to give comment if you have doubt.

Thanks for visiting.

Magento order invoice pdf in customer account dashboard
Tagged on:                                 

5 thoughts on “Magento order invoice pdf in customer account dashboard

  • June 3, 2016 at 12:26 pm
    Permalink

    Hello,
    I just stumbled on your article while looking for a way to get the invoice via url in Magento.
    I followed step by step your tutorial and succeed to get the right link in client dashboard.
    Nonetheless, the link redirected to a 404 page.
    Could you have a clue about the why ?
    Thanks a lot for this article and thanks in advance for any help you could provide.

    Reply
    • June 4, 2016 at 8:30 am
      Permalink

      First of all this tutorial is for the accessing pdf in frontend not backend. So If you are trying to access in backend through custom URL then you need to follow the basis structure of magento backend URL structure I mean all URL needs to be go through magento admin URL else if you are trying to access it through frontend URL then first please check whether your router or controller is pointing or not or you can download full module from this article then check how is works then go for your custom URL.

      Thanks

      Reply
      • June 6, 2016 at 7:05 pm
        Permalink

        I did a clean reinstall of the module from the files you suggest to download, ans everything is working fine.
        Thank you very much !!

        Reply
  • July 24, 2016 at 3:32 pm
    Permalink

    Thanks a lot. It works fine on RWD 1.9.2.4. (mix beetween yours phtml and native rwd).
    I dont know if it is a mistake but the “view order” is not present in modified phtml.

    Reply
    • July 24, 2016 at 5:41 pm
      Permalink

      Actually there is no mistaken been done from your side . You need to just copy my logic to your custom theme, Not whole phtml file . I have explained which line needs t be changed to which one. So again please check the article again and just copy some code which need be but not whole phtml file.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *