We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Magento 2 has many templates to send when the customer changes the order process like an invoice, shipment, credit memo, etc. But the same isn’t available on the cancellation process.

    Today, we’re going to show you how to set it up for the cancelation process.

    Module name: Magemonkeys_Cancelledorder

    Create events.xml at app/code/Magemonkeys/Cancelledorder/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="sales_order_save_after">
            <observer name="sales_order_save_after"
                      instance="MagemonkeysCancelledorderObserverOrderSaveAfter"/>
        </event>
    </config>

    Then Create OrderSaveAfter.php at app/code/Magemonkeys/Cancelledorder/Observer/

    <?php
    namespace MagemonkeysCancelledorderObserver;
    
    use MagentoFrameworkEventObserverInterface;
    use MagentoSalesModelOrderEmailSenderOrderCommentSender;
    
    class OrderSaveAfter implements ObserverInterface
    {
    
        protected $orderCommentSender;
    
        public function __construct(
            OrderCommentSender $orderCommentSender
        )
        {
            $this->orderCommentSender = $orderCommentSender;
        }
    
        public function execute(MagentoFrameworkEventObserver $observer)
        {
            $order = $observer->getEvent()->getOrder();
            if ($order->getState() == 'canceled') {
                $this->orderCommentSender->send($order, true);
            }
        }
    }

    The mail will look something like the below.

    Hope that helps.

    field_5bfb909c5ccae

      Get a Free Quote