2

I want to add a custom parameter in the invoice template and display that template. so please give me any solution on how to do that?

2 Answers 2

0

There is an observer for passing custom parameters to invoice template. Try the below one in your custom module.

Vendor/Module/etc/events.xml

<event name="email_invoice_set_template_vars_before">
    <observer name="vendor-add-custom-field-data-invoice" instance="Vendor\Module\Model\Observer\Invoice\EmailTemplateVars"/>
</event>

Vendor/Module/Model/Observer/Invoice/EmailTemplateVars.php

namespace Vendor\Module\Model\Observer\Invoice;

class EmailTemplateVars implements \Magento\Framework\Event\ObserverInterface
{
    protected $helper;

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $transport = $observer->getTransport();
       $order = $transport['order'];

       $transport['my_custom_field'] = "test";        
    }
} 
3
  • Thank You for your response but not working for me.. Commented Sep 18, 2019 at 9:36
  • did you clear cache after creating module and setup upgrade??
    – Vishnunath
    Commented Sep 19, 2019 at 12:34
  • thanks, I have run command but not working for me. Commented Oct 2, 2019 at 5:37
0

Vendor/Module/etc/events.xml

<event name="email_invoice_set_template_vars_before">
  <observer name="vendor-add-custom-field-data-invoice" instance="Vendor\Module\Model\Observer\Invoice\EmailTemplateVars"/>
</event>

Vendor/Module/Model/Observer/Invoice/EmailTemplateVars.php

namespace Vendor\Module\Model\Observer\Invoice;

class EmailTemplateVars implements \Magento\Framework\Event\ObserverInterface
{
    protected $helper;
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $transportObject = $observer->getEvent()->getData('transportObject');
       $order = $transportObject->getData('order');
       $transportObject->setData('my_custom_field', 'test');
    }
}

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.