1

I have created a observer on "email_shipment_set_template_vars_before" & trying to set some template var but its not working.

any thoughts ?

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Mod\Sales\Observer;

use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
use Magento\Sales\Model\Order\Email\Container\Template;

class ShipmentSender implements ObserverInterface
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(LoggerInterface $logger,Template $templateContainer)
    {
        $this->logger = $logger;
        $this->templateContainer = $templateContainer;
    }

    /**
     * @param Observer $observer
     * @param Template $templateContainer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $transport = $observer->getEvent()->getTransport();
        $shipment  = $transport['shipment'];
        $order  = $transport['order'];
        $justshipped = '45';
        $shipped = '99';
        $transport['shipment'] = $shipment;
        $transport['justshipped'] = $justshipped;
        $transport['shipped'] = $shipped;
        $this->templateContainer->setTemplateVars($transport);
    }
}

adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_shipment_set_template_vars_before">
        <observer name="shipment_sender" instance="Mod\Sales\Observer\ShipmentSender" />
    </event>
</config>
4
  • I am not getting any data in email template for template vars i set here.
    – devhs
    Commented Feb 19, 2021 at 7:12
  • any thoughts ??
    – devhs
    Commented Feb 19, 2021 at 10:12
  • I have added answer. It is perfectly works fine. I have checked on it by doing practical. Please let me know if you have any query.
    – ZealousWeb
    Commented Feb 23, 2021 at 5:25
  • Have you implemented answer?
    – ZealousWeb
    Commented Feb 26, 2021 at 5:45

1 Answer 1

7

Yes , i have found a way for it. Please follow below steps to achieve this.

Step 1: Create events.xml file under path PackageName/Module/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_shipment_set_template_vars_before">
        <observer name="add_Custom_variable_to_Order" 
            instance="PackageName\Module\Observer\ObserverforAddCustomVariable" />
    </event>
</config>

Step 2: Create file ObserverforAddCustomVariable.php under path PackageName/Module/Observer/

<?php
namespace PackageName\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order\Email\Sender\ShipmentSender;

class ObserverforAddCustomVariable implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $sender = $observer->getData('sender');
        if (!$sender instanceof ShipmentSender) {
            return;
        }

        /** @var DataObject $transport */
        $transport = $observer->getData('transportObject');
        $transport->setData('CustomVariable1', 'Your Value1');
        $transport->setData('CustomVariable2', 'Your Value2');
        return;
    }
}

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

Input - https://prnt.sc/103maal Output - https://prnt.sc/103mb56

Like above , you can set your own custom variables. It will surely work for you.

Thanks! Cheers

15
  • Have you checked answer? @devhs
    – ZealousWeb
    Commented Feb 22, 2021 at 12:05
  • This answer appears to be great, will test soon.
    – devhs
    Commented Feb 24, 2021 at 4:57
  • Thanks, after that can you please approve answer?
    – ZealousWeb
    Commented Feb 24, 2021 at 5:13
  • 1
    Sure, let me check if it works i will mark answer as right in this question.
    – devhs
    Commented Feb 24, 2021 at 5:17
  • 1
    Working fine...!!! Commented Aug 27, 2021 at 6:06

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.