1

I want to get download invoice PDF link in customer dashboard account, when they are inside their order.

Therefore I need to edit default code in file: /app/design/frontend/theme/theme/template/sales/order/info/buttons.phtml

The default code is to download order PDF, but I want to change that to Invoice PDF.

Default code:

<a href="<?php echo $this->getPrintUrl($_order) ?>" class="link-print" onclick="this.target='_blank';"></a>

How can I achieve that?

2 Answers 2

1

You can add Invoice link directly

Please check below example.

Create one Front Controller

class [NameSpace]_[ModuleName]_IndexController extends Mage_Core_Controller_Front_Action
{
    protected function _initInvoice($update = false)
    {
        $this->_title($this->__('Sales'))->_title($this->__('Invoices'));
        $invoice = false;
        $itemsToInvoice = 0;
        $invoiceId = $this->getRequest()->getParam('invoice_id');
        $orderId = $this->getRequest()->getParam('order_id');
        if ($invoiceId) {
            $invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
            if (!$invoice->getId()) {
                $this->_getSession()->addError($this->__('The invoice no longer exists.'));
                return false;
            }
        } elseif ($orderId) {
            $order = Mage::getModel('sales/order')->load($orderId);
            /**
             * Check order existing
             */
            if (!$order->getId()) {
                $this->_getSession()->addError($this->__('The order no longer exists.'));
                return false;
            }
            /**
             * Check invoice create availability
             */
            if (!$order->canInvoice()) {
                $this->_getSession()->addError($this->__('The order does not allow creating an invoice.'));
                return false;
            }
            $savedQtys = $this->_getItemQtys();
            $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys);
            if (!$invoice->getTotalQty()) {
            Mage::throwException($this->__('Cannot create an invoice without products.'));
            }
        }

        Mage::register('current_invoice', $invoice);
        return $invoice;
    }

    protected function print()
    {
        if ($invoiceId = $this->getRequest()->getParam('invoice_id')) {
            if ($invoice = Mage::getModel('sales/order_invoice')->load($invoiceId)) {
                $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($invoice));
            $this->_prepareDownloadResponse('invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').
                '.pdf', $pdf->render(), 'application/pdf');
            }
        }
        else {
            $this->_forward('noRoute');
        }
    }

    public function printAction()
    {
        $this->_initInvoice();
        $this->print();
    }
}

Edit items.phtml file and put below linke

Go to /app/design/frontend/theme/theme/template/sales/order/invoice/items.phtml

<h2 class="sub-title">
<?php echo $this->__('Invoice #') ?><?php echo $_invoice->getIncrementId(); ?> <span class="separator">|</span> <a href="<?php echo $this->getPrintInvoiceUrl($_invoice) ?>" onclick="this.target='_blank'" class="link-print"><?php echo $this->__('Print Invoice') ?></a>
<!-- Add Invoice PDF Link  -->
<span class="separator">|</span> <a href="<?php echo $this->getUrl('[frontRouterName]/index/print',array('invoice_id'=>$_invoice->getId(),'order_id'=>$_order->getId())) ?>" onclick="this.target='_blank'" class="link-print"><?php echo $this->__('Print PDF') ?></a>
</h2>

You can directly download Invoice pdf

1
  • I used this , very nice. But "print()" is reserved keyword in PHP so I had to rename to "printIt()" Aug 28, 2019 at 14:13
0

C:\xampp\htdocs\m1\app\etc\modules\Maddy_Custompdf.xml

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

C:\xampp\htdocs\m1\app\code\local\Maddy\Custompdf\IndexController.php

<?php

class Maddy_Custompdf_IndexController extends Mage_Core_Controller_Front_Action {        

    public function invoicesAction() {
        $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'
                );

            }
        }
    }

    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;
    }
 }

C:\xampp\htdocs\m1\app\code\local\Maddy\Custompdf\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Maddy_Custompdf>
            <version>0.1.0</version>
        </Maddy_Custompdf>
    </modules>
    <frontend>
        <routers>
            <custompdf>
                <use>standard</use>
                <args>
                    <module>Maddy_Custompdf</module>
                    <frontName>custompdf</frontName>
                </args>
            </custompdf>
        </routers>
        <layout>
            <updates>
                <custombrand>
                    <file>custombrand.xml</file>
                </custombrand>
            </updates>
        </layout>
    </frontend>
  </config>

C:\xampp\htdocs\m1\app\design\frontend\base\default\template\sales\order\history.php

Add this line on line # 63 around below Reorder.

<strong><a href="<?php echo $this->getUrl('custompdf/index/invoices', array('order_id' => $_order->getId())) ?>">Print Invoices</a></strong>

It will surely Help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.