I know the cancel order event order_cancel_after
but how can I send cancelation e-mail to the customer when admin cancels their order from admin.
How to write a custom extension for this.
Any help would be appreciated.
Thanks in advance..!
I know the cancel order event order_cancel_after
but how can I send cancelation e-mail to the customer when admin cancels their order from admin.
How to write a custom extension for this.
Any help would be appreciated.
Thanks in advance..!
Create an event file in Vendor/Module/etc/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='order_cancel_after'>
<observer name='order_cancel_after' instance='Vendor\Module\Observer\CancelOrder'/>
</event>
</config>
Create email template xml in etc/email_templates.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Email/etc/email_templates.xsd">
<template id="cancel_order_template" label="Email Form" file="cancelordertemplate.html" type="html" module="Vendor_Module" area="frontend"/>
</config>
Create an email template in view/frontend/email/cancelordertemplate.html
<!--@subject {{var template_subject}} @-->
{{template config_path="design/email/header_template"}}
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td align="center" valign="top" style="padding:10px 0 10px 0">
<table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650">
<tr>
<td>
<table cellspacing="0" cellpadding="0" border="0" width="650">
<tbody>
<tr>
<td colspan="2" valign="top" style="font-size:13px;padding:0 9px 9px 9px;">
<p>{{trans "Dear"}} {{var customername}},</p>
{{var email_content|raw}}
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
{{template config_path="design/email/footer_template"}}
Create an observer file in Vendor/Module/Observer/CancelOrder.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Framework\Mail\Template\TransportBuilder;
use \Magento\Framework\Translate\Inline\StateInterface;
use Psr\Log\LoggerInterface;
class CancelOrder implements ObserverInterface
{
public function __construct(
StoreManagerInterface $storeManager,
\Magento\Framework\Message\ManagerInterface $messageManager,
TransportBuilder $transportBuilder,
LoggerInterface $logLoggerInterface,
StateInterface $inlineTranslation
) {
$this->storeManager = $storeManager;
$this->messageManager = $messageManager;
$this->transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->logLoggerInterface = $logLoggerInterface;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
try {
$order = $observer->getEvent()->getOrder();
$custFirstname = ''; # Set customer name from order
$custEmail = ''; # Set customer email from order
$template_sub = ''; # Set email subject
$sender_name = ''; # Set sender name
$sender_email = ''; # Set sender email
$template_content = ''; # Set email content
$templateId = 'cancel_order_template';
$this->inlineTranslation->suspend();
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$sendFrom = $sender_email;
$senderName = $sender_name;
$sendTo = $custEmail;
$transport = $this->transportBuilder
->setTemplateIdentifier($templateId)
->setTemplateOptions(
[
'area' => 'frontend',
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)
->setTemplateVars([
'template_subject' => $template_sub,
'customername' => $custFirstname,
'email_content' => $template_content,
])
->setFrom(array('email' => $sendFrom, 'name' => $senderName))
->addTo(array($sendTo))
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
} catch (\Exception $e) {
$this->logLoggerInterface->debug($e->getMessage());
exit;
}
}
}
Clear cache: php bin/magento cache:clean
and check it.