4

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..!

2
  • You need to send a custom email to customer. Right? Commented Nov 6, 2019 at 9:52
  • @ Dhaduk Mitesh, Yeah you right. Commented Nov 6, 2019 at 10:04

1 Answer 1

2

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.

7
  • 1
    I will check and let you know. Commented Nov 6, 2019 at 10:39
  • 1
    I am facing this error See this screenshot: prnt.sc/pt6q40 In this Method : ($this->canCancel()) Can you please guide me in this. Commented Nov 6, 2019 at 11:24
  • 1
    Check the updated answer. Remove that condition. Commented Nov 6, 2019 at 11:26
  • 1
    Currently I am checking and I will let you know if it works. Commented Nov 7, 2019 at 4:04
  • 1
    @ DhadukMitesh, Thanks for answer it works for me..!! Commented Nov 7, 2019 at 6:30

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.