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" />

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.


3 Answers 3


Update observer execute method.

public function execute(\Magento\Framework\Event\Observer $observer)
    /** @var \Magento\Framework\App\Action\Action $controller */
    $transportObject = $observer->getEvent()->getData('transportObject');
    $order = $transportObject->getData('order');
    $transportObject->setData('businessname', 'XYZ');
  • 1
    It works! Thanks @Naeem. Commented Jul 25, 2022 at 9:56

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

 It's a bug of Magento as wrote in the comment of your question.

I resolved the problem  in this way (but I changed order comments email vars, but use the same changes for invoice core classes):

1) I created a preference class for the core class because it is a bad way to write code in the core file.
- added in di.xml code

      <preference for="Magento\Sales\Model\Order\Email\Sender\OrderCommentSender" type="Namespace\ExtensionName\Model\Order\Email\Sender\OrderCommentSender" />

2) Fixed source code in my class. The main idea: we added prepare template and we need to extend source \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender to prevent another core error.

     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
    namespace Sfdev\SecretDelivery\Model\Order\Email\Sender;
    use Magento\Framework\Oauth\Exception;
    use Magento\Sales\Model\Order;
    use Magento\Sales\Model\Order\Email\Container\OrderCommentIdentity;
    use Magento\Sales\Model\Order\Email\Container\Template;
    use Magento\Sales\Model\Order\Email\NotifySender;
    use Magento\Sales\Model\Order\Address\Renderer;
    use Magento\Framework\Event\ManagerInterface;
     * Class OrderCommentSender
    class OrderCommentSender extends **\Magento\Sales\Model\Order\Email\Sender\OrderCommentSender**
         * @var Renderer
        protected $addressRenderer;
         * Application Event Dispatcher
         * @var ManagerInterface
        protected $eventManager;
        **public $comment = '';**
         * @param Template $templateContainer
         * @param OrderCommentIdentity $identityContainer
         * @param Order\Email\SenderBuilderFactory $senderBuilderFactory
         * @param \Psr\Log\LoggerInterface $logger
         * @param Renderer $addressRenderer
         * @param ManagerInterface $eventManager
        public function __construct(
            Template $templateContainer,
            OrderCommentIdentity $identityContainer,
            \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory,
            \Psr\Log\LoggerInterface $logger,
            Renderer $addressRenderer,
            ManagerInterface $eventManager
        ) {
            parent::__construct($templateContainer, $identityContainer, $senderBuilderFactory, $logger, $addressRenderer, $eventManager);
            $this->addressRenderer = $addressRenderer;
            $this->eventManager = $eventManager;
         * Send email to customer
         * @param Order $order
         * @param bool $notify
         * @param string $comment
         * @return bool
        public function send(Order $order, $notify = true, $comment = '')
            **$this->comment = $comment;**
            return $this->checkAndSend($order, $notify);
         * Prepare email template with variables
         * @param Order $order
         * @return void
        **protected function prepareTemplate(Order $order)**
            $transport = [
                'order' => $order,
                'comment' => $this->comment,
                'billing' => $order->getBillingAddress(),
                'store' => $order->getStore(),
                'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
            $transport = new \Magento\Framework\DataObject($transport);
                ['sender' => $this, 'transport' => $transport]
            try {
            } catch (\Exception $e) {
                $this->logger->error('ERROR EMAIL VARS');

3) The result is - we can use core dispatch 'email_order_comment_set_template_vars_before' - it is working correctly

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.