Magento – Add custom email variable to invoice email

email-templatesinvoicemagento2

I am referring to question –
Magento 2 : How to add custom data in order email

Below is by observer execute method

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var \Magento\Framework\App\Action\Action $controller */
    $transport = $observer->getTransport();
    $transport['businessname'] = "XYZ";
}

Below is my event.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_invoice_set_template_vars_before">
        <observer name="custom_invoice_email_variable" instance="VendorName\ModuleName\Observer\SalesInvoiceTemplateVarsBefore" />
    </event>
</config>

I am calling it in a template as – {{var businessname|raw}}. But still, I am not getting it in the email template. I checked by putting logs in the observer and my observer method is getting called, But the variable does not get set and is not shown in the invoice template.

Please help.

Best Answer

The problem is that you are not calling the right event. It should be:

<event name="email_order_set_template_vars_before">

I think the event you are observing is when you are sending email by "Asynchronous sending".enter image description here