2

I am referring to question - how to add custom data in order email in magento 2

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 observer and my observer method is getting called, But the variable is not get set and not shown in invoice template.

Please help.

0

Its 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 same changes for invoice core classes):

1) I created preference class for core class because it bad way write code in 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 prepareTemplate and we need extend source \Magento\Sales\Model\Order\Email\Sender\OrderCommentSender to prevent another core error.

<?php
    /**
     * 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);

            $this->eventManager->dispatch(
                'email_order_comment_set_template_vars_before',
                ['sender' => $this, 'transport' => $transport]
            );

            try {
                $this->templateContainer->setTemplateVars($transport->getData());

                parent::prepareTemplate($order);
            } catch (\Exception $e) {

                $this->logger->error('ERROR EMAIL VARS');
                $this->logger->error($e->getMessage());
            }

        }

    }

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

0

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

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.