0

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.

0

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

https://www.mageplaza.com/magento-2-module-development/how-create-hello-world-module-magento-2.html

https://devdocs.magento.com/videos/fundamentals/create-a-new-module/

https://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/

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"
                type="Vendor\Module\Model\InvoiceData"/>
</config>

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

<?php
declare(strict_types=1);
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)
    {  
        $invoice->setSendEmail($this->identityContainer->isEnabled());

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

            $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.
             */
            $this->eventManager->dispatch(
                'email_invoice_set_template_vars_before',
                ['sender' => $this, 'transport' => $transportObject->getData(), 'transportObject' => $transportObject]
            );

            $this->templateContainer->setTemplateVars($transportObject->getData());

            if ($this->checkAndSend($order)) {
                $invoice->setEmailSent(true);
                $this->invoiceResource->saveAttribute($invoice, ['send_email', 'email_sent']);
                return true;
            }
        } else {
            $invoice->setEmailSent(null);
            $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>
1
  • Thank you. I 've used an Observer to solve this problem. I am going to put my solution here
    – user95895
    Apr 27 '21 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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