2

I like to send emails for my sales order.

  1. If the order is placed status as "Processing". (email send automatically) it's okay
  2. Processing to "Dispatch" (not having the option to set dispatch)
  3. Dispatch to "Shipments" (this too I assigned email with tracking details)
  4. Shipments to "Delivered" (not having option to set delivered)
  5. For all this order I need to send emails to customers.
  • there is a checkbox option about Submit button related to Notification to customer. – Hardik Visa Jun 29 '19 at 7:17
  • Yeah i know that, but it directlly goes from "Processing" to "shipments" – Priya Jun 29 '19 at 7:24
3
+50

For that you have to use event sales_order_state_change_before and check previous state/status and current state/status. According to this pass data in template variable and use in email template.

<?php

namespace Vendor\Namespace\Observer;

use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\Template\TransportBuilder;

class Statechange implements \Magento\Framework\Event\ObserverInterface {

    protected $inlineTranslation;
    protected $escaper;
    protected $transportBuilder;
    protected $logger;

    public function __construct(        
        StateInterface $inlineTranslation,
        Escaper $escaper,
        TransportBuilder $transportBuilder,
        \Psr\Log\LoggerInterface $logger
    ) {

        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->logger = $logger;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $transport = $observer->getEvent()->getTransport();
        $previousState = $order->getState();
        $currentState = $transport->getState();
        if ($order instanceof \Magento\Framework\Model\AbstractModel) {
           if($previousState == 'processing ' && $currentState == 'complete') {
                // processing to complete logic here
                $this->sendEmail($order,$previousState,$currentState);
           }else if($previousState == 'new' && $currentState == 'cancel') {
                // new to cance logic here
                $this->sendEmail($order,$previousState,$currentState);
           }
        }
        return $this;
    }
    public function sendEmail($order,$previousState,$currentState)
    {
            try {
                $this->inlineTranslation->suspend();
                $sender = [
                    'name' => $this->escaper->escapeHtml('Test'),
                    'email' => $this->escaper->escapeHtml('abc@gmail.com'),
                ];
                $transport = $this->transportBuilder
                    ->setTemplateIdentifier('email_demo_template')
                    ->setTemplateOptions(
                        [
                            'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                            'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                        ]
                    )
                    ->setTemplateVars([
                        'order'  => $order,
                        'previousState'  => $previousState,
                        'currentState'  => $currentState,
                    ])
                    ->setFrom($sender)
                    ->addTo($order->getCustomerEmail())
                    ->getTransport();
                $transport->sendMessage();
                $this->inlineTranslation->resume();
            } catch (\Exception $e) {
                $this->logger->debug($e->getMessage());
            }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.