PDF invoice in order confirmation email

As everyone who uses Magento knows, in most cases after each order, the customer receives an order confirmation email which contains some useful information about an order that has been just placed. However, sometimes, it would be great to provide some additional information alongside with an order confirmation – such as payment information. There are some ways in Magento for extending emails with custom info, but often they require a lot of time and deep technical knowledges. Let’s leave these ways for people who has an extra time and review the simple and fast way how to add an invoice information to the order confirmation email.

So, we have many ways to extend an email with payment information. There’s possibility to add the additional blocks directly to the email content but, believe us, it is not the worth of the wasted time. In this case, Magento has built in the ability to generate an invoice PDF, and it means we can use that ability for our own needs. It’s not a big deal to add an attachment to almost any email in Magento using native core methods.

The main problem is we can attach an invoice information only when the order has an invoice. As you may know, for some payment methods the payment process may be delayed. It takes place some time after the order has been created in Magento. If you use such payment methods, the article might not be interesting for you. Otherwise, let’s see what we can do.

First of all, we need to find the logic that is responsible for sending emails. Fortunately, in our case, we need to interact only with one class Mage_Core_Model_Email_Template_Mailer. There you can find the method named send() where the final email data is being generated. In this method, we need to ensure that the email, which is taking place at the moment, is an order confirmation email since the send() method is being used for sending almost all emails and, honestly, we don’t want to try to send an invoice each time when a new customer has been registered or forgot his password. Please note that the examples in this article are supposed that you already know how to create a custom extension and override the standard Magento methods. However, you will find the complete extension in the end of this article.

So, let’s override the send() method of the Mage_Core_Model_Email_Template_Mailer class mentioned above:

public function send()
    {
        $emailTemplate = Mage::getModel('core/email_template');

        /* Check if now we are sending order confirmation email */
        $storeId = $this->getStoreId();
        if ($this->getTemplateId() == Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId)
            || Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_TEMPLATE, $storeId)) {
            $templateParams = $this->getTemplateParams();
            if (isset($templateParams['order'])) {
                /** @var $order Mage_Sales_Model_Order */
                $order = $templateParams['order'];
                $invoices = $order->getInvoiceCollection();
                $invoicesSet = array();
                foreach ($invoices as $_invoice) {
                    array_push($invoicesSet, $_invoice);
                }
                /* Add pdf invoice to the order confirmation email */
                if (count($invoicesSet) > 0) {
                    $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoicesSet);
                    $this->addAttachment($emailTemplate, $pdf, 'invoice.pdf');
                }
            }
        }

        // Send all emails from corresponding list
        while (!empty($this->_emailInfos)) {
            $emailInfo = array_pop($this->_emailInfos);
            // Handle "Bcc" recepients of the current email
            $emailTemplate->addBcc($emailInfo->getBccEmails());
            // Set required design parameters and delegate email sending to Mage_Core_Model_Email_Template
            $emailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $this->getStoreId()))
                ->sendTransactional(
                $this->getTemplateId(),
                $this->getSender(),
                $emailInfo->getToEmails(),
                $emailInfo->getToNames(),
                $this->getTemplateParams(),
                $this->getStoreId()
            );
        }
        return $this;
    }

Additionally, we need to add one more method to add an email attachment. We will call the method addAttachment:

/**
     * Adds an attachment to the current email template
     *
     * @param Mage_Core_Model_Email_Template $template
     * @param Zend_Pdf $pdf
     * @param string $filename
     * @return Atwix_ConfirmationInvoice_Model_Email_Template_Mailer
     */
    public function addAttachment($template, Zend_Pdf $pdf, $filename)
    {
        $file = $pdf->render();
        $attachment = $template->getMail()->createAttachment($file);
        if(!is_object($attachment)) return $this;
        $attachment->type = 'application/pdf';
        $attachment->filename = $filename;

        return $this;
    }

As you can see, in the send() method we check the $templateParams for the presence of ‘order’ params – this step let us know that we are working with an order email at the moment. So, if the order param exists – we simply collect the order invoice information, generate PDF file using the standard Magento (Zend) set of methods and add the generated PDF to the order confirmation email.

Good news:  you need nothing more than override one core method to insert a PDF invoice into a confirmation email. You can also try to make the extension yourself based on the above examples. The other way is that you can download the extension here: https://github.com/Atwix/confirminvoice
All the questions and proposals in the comments are always appreciated. Good luck!

Read more: