I am overriding OrderSender class for add custom variable in order confirmation mail. This variable's value is dynamic.

but this file conflicts and give an error like

"Type Error occurred when creating object: Magento\Sales\Model\OrderNotifier".

I want to add a custom variable in the order confirmation mail.

I override the class using di.xml

 <?xml version="1.0"?>
  <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\OrderSender" type="Vendor\Module\Model\Order\Email\Sender\OrderSender" />

Any solution is appreciated.

Thanks in advance

  • email_order_set_template_vars_before event for Add custom data in order email – Rk Rathod Jun 18 '19 at 11:34
  • you can set custom data using event – Rk Rathod Jun 18 '19 at 11:35
  • I tried event and observer also but it does not work for me can you please post the code for me. – Pratik ojha Jun 18 '19 at 11:36
  • please check my answer – Rk Rathod Jun 18 '19 at 11:41
  • I have to override order_new.html file in my custom module or not? – Pratik ojha Jun 18 '19 at 11:44


<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_order_set_template_vars_before">
        <observer name="add_Custom_variable_to_Order" 
            instance="[Vendor]\[ModuleName]\Observer\ObserverforAddCustomVariable" />

Observer file:

namespace [Vendor]\[ModuleName]\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverforAddCustomVariable implements ObserverInterface

    public function __construct(
    ) {

     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(\Magento\Framework\Event\Observer $observer)
        /** @var \Magento\Framework\App\Action\Action $controller */
        $transport = $observer->getTransport();
        $transport['CustomVariable1'] = 'Your Value1 ';
        $transport['CustomVariable2'] = 'Your Value2';

At the email template , you can get this custom variables CustomVariable1,CustomVariable2 using {{var CustomVariable1|raw}},{{var CustomVariable2|raw}}

Reference Link

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.