How to Send Email After Order Cancellation in Magento 2
The default Magento 2 allows the merchants to set up order confirmation Email in Magento 2, and also for the shipment and invoice generation. These email notifications are very important for the better customer shopping experience.
Sometimes, due to the stock unavailability or any other reasons, the admin has to cancel the order from the backend. However, when an order is cancelled from the backend, the order cancellation email is not sent to the customers. Therefore, if the admin cancels the order, and the customer does not receive an email. It affects shopping experience and lower the customer retention rate.
Today, I’ve come up with the solution to send email after order cancellation in Magento 2 to better notify the customers for their cancelled orders.
Programmatic Solution to Send Email after Order Cancellation in Magento 2
- Create events.xml at app/code/Vendor/Extension/etc/
12345678<?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="Vendor\Extension\Observer\OrderSaveAfter"/></event></config> - Create OrderSaveAfter.php at app/code/Vendor/Extension/Observer/
123456789101112131415161718192021222324252627<?phpnamespace Vendor\Extension\Observer;use Magento\Framework\Event\ObserverInterface;use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;class OrderSaveAfter implements ObserverInterface{protected $orderCommentSender;public function __construct(OrderCommentSender $orderCommentSender){$this->orderCommentSender = $orderCommentSender;}public function execute(\Magento\Framework\Event\Observer $observer){$order = $observer->getEvent()->getOrder();if ($order->getState() == 'canceled') {$this->orderCommentSender->send($order, true);}}}
Done!
Implementing the above solution automatically sends email notification to customers after the order is cancelled from the backend.
If you have a query regarding this post, feel free to ask in the Comment section below.
I would like to solve your query.
Do consider sharing this post with Magento Community via social media.
Thank you.
Related Posts:
- How to Configure Invoice Emails in Magento 2
- How to Configure Shipment Emails in Magento 2
- How to Setup Email Refer a Friend in Magento 2
- How to Cancel Order in Magento 2
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hi,
I tried to edit the file /magento/module-sales/Model/Order/Email/NotifySender.php
where line 28 checks whether send comment email is enabled:
if (!$this->identityContainer->isEnabled()) {
return false;
}
$this->prepareTemplate($order);
I tried this adjustment:
if (! $ this-> identityContainer-> isEnabled ()) {
if (! $ order-> getState () == ‘canceled’) {
return false;
}
$this->prepareTemplate($order);
An email is also sent when disabled send comment is disabled and order canceled.
Do you have an idea how to incorporate it into your solution? I don’t have such experience in php.
Regards
Eduard
Hello,
It is not advisable to make changes to the core file. You need to send a custom email.
Thanks.
Hi,
In the settings I have Sales emails-Order Comment-Enabled = No. Your solution in this case does not work.
If Sales emails-Order Comment-Enabled = Yes, Your solution in this case does work.
I do not want to change this setting (change Order Comment-Enabled to Yes) , but I only want an email to be sent when order canceled.
How do I set it up in OrderSaveAfter? Can only be inserted there for this case const: XML_PATH_EMAIL_ENABLED, “1” so that the email comment is not sent in another case.
Regards
Eduard
Hello,
As you are using a comment sender mail template, you cannot send an email when the comment is selected as ‘no’.
You need to send the email programmatically using the method at https://meetanshi.com/blog/send-custom-emails-programmatically-in-magento-2/
Thank you.