3

I created a new template var using email_order_set_template_vars_before observer and it works when I send email new order through the Magento admin. But it does not trigger when new order is placed. Here is my code.

events.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="shipperhq_save_email_variables" disabled="true"/> -->
        <observer name="cd_shipperhq_save_email_variables" instance="Foobar\ShipperHQDeliveryDate\Observer\SaveEmailVariables"/>
    </event>

</config>

SaveEmailVariables.php

namespace Foobar\ShipperHQDeliveryDate\Observer;
use Magento\Framework\Event\ObserverInterface;

class SaveEmailVariables implements ObserverInterface
{
    private $carrierGroupHelper;
    public function __construct(\ShipperHQ\Shipper\Helper\CarrierGroup $carrierGroupHelper) {
        $this->carrierGroupHelper = $carrierGroupHelper;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $transport = $observer->getEvent()->getTransport();
        $order = $transport->getOrder();
        $orderDetail = $this->carrierGroupHelper->getOrderCarrierGroupInfo($order->getId());

        if($order != null)
        {
            foreach ($orderDetail as $orderData) {
                $ddate = array_key_exists('delivery_date', $orderData) ? $orderData['delivery_date'] : '';

                $transport['cddeliveryDate'] = date( 'F d, Y', strtotime( $ddate ) );

            }
        }
    }
}```
4
  • check this link magento.stackexchange.com/questions/182051/… May 6, 2020 at 18:13
  • Is that events.xml file under "app/code/Foobar/ShipperHQDeliveryDate/etc/adminhtml/"?
    – kunj
    May 12, 2020 at 5:26
  • I have a question about your order emails. What is your value on sales_email/general/async_sending? Are emails being sent on submit or being triggered later on? May 14, 2020 at 7:42
  • please create event.xml in global. it's maybe due to a scope issue. Mar 22, 2022 at 17:28

1 Answer 1

1

The described behaviour point to wrong placed the events.xml file in the directory hierarchy. If you want to catch dispatched event globally (in frontend and admin) you need to declare events.xml file under [module_name]/etc directory. In your case it'll be:

app/code/Foobar/ShipperHQDeliveryDate/etc/events.xml

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.