I need to override invoice email template and add a variable (po number) in the template. I ve followed a few tutorials but they didn't help at all. I have an email template in admin panel at Marketing > email templates . I am new to Magento and don t know how to find this file and associated files in source code. Thank you

Edit: html/vendor/magento/module-sales/Model/Order/Email/Sender/InvoiceSender.php

i guess this is the file that i have to add an observer and push the data in transport object. I couldn't figure out how to do it though.

1 Answer 1


To achieve this first step you should do is create sample module,I'm dropping reference links for that.




For current problem statement you have to create only required files that'll be, registration.php, etc/module.xml

After you enable your module, Refer next steps:

Create file di.xml under etc directory with below content:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Email\Sender\InvoiceSender"

Create file InvoiceData.php under Vendor\Module\Model directory and add below contents:

namespace Vendor\Module\Model;

use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Sales\Model\Order\Invoice;

class InvoiceData extends InvoiceSender
     * @param Invoice $invoice
     * @param false $forceSyncMode
     * @return bool
     * @throws \Exception
    public function send(Invoice $invoice, $forceSyncMode = false)

        if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
            $order = $invoice->getOrder();

            $transport = [
                'order' => $order,
                'invoice' => $invoice,
                'comment' => $invoice->getCustomerNoteNotify() ? $invoice->getCustomerNote() : '',
                'billing' => $order->getBillingAddress(),
                'payment_html' => $this->getPaymentHtml($order),
                'store' => $order->getStore(),
                'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
                'po_number' => 'Your PO Number Vaule'
            $transportObject = new \Magento\Framework\DataObject($transport);

             * Event argument `transport` is @deprecated. Use `transportObject` instead.
                ['sender' => $this, 'transport' => $transportObject->getData(), 'transportObject' => $transportObject]


            if ($this->checkAndSend($order)) {
                $this->invoiceResource->saveAttribute($invoice, ['send_email', 'email_sent']);
                return true;
        } else {
            $this->invoiceResource->saveAttribute($invoice, 'email_sent');

        $this->invoiceResource->saveAttribute($invoice, 'send_email');

        return false;

After that clear cache and in your invoice template you can use variable as below:

<p style="font-size: 14px;"> {{var po_number|raw}}</p>
  • Thank you. I 've used an Observer to solve this problem. I am going to put my solution here
    – user95895
    Commented Apr 27, 2021 at 21:18

Your Answer

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

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