0

I am trying to use different e-mail template for one of my custom order status when the status is changed from the admin.

I have used the event sales_order_save_after

<observer name="sales_order_save_after" instance="Hawk\OrderFlow\Observer\Sales\Order\SalesOrderAfterSave"/>

And created an observer like follows:

<?php

namespace Hawk\OrderFlow\Observer\Sales\Order;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class SalesOrderAfterSave implements ObserverInterface {

/**
 * @var \Magento\Framework\App\Request\Http
 */
protected $_request;

/**
 * @var \Magento\Framework\Mail\Template\TransportBuilder
 */
protected $_transportBuilder;

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $_storeManager;

/**
 * For logger, var/log
 */
protected $_logger;


public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Psr\Log\LoggerInterface $logger
) {
    $this->_request = $request;
    $this->_transportBuilder = $transportBuilder;
    $this->_storeManager = $storeManager;
    $this->_logger = $logger;
}

public function execute(\Magento\Framework\Event\Observer $observer)

{

    $order = $observer->getEvent()->getOrder();
    $lastOrderId = $order->getId();
    $orderStatus = $order->getStatus();

    try {

        if($orderStatus  == "viewed_by_admin") {
            $store = $this->_storeManager->getStore()->getId();
            $template = $this->_transportBuilder->setTemplateIdentifier('13') //Where 13 is the Email Template ID created in the admin pannel under the Stores -> Email Templates
            ->setTemplateOptions(['area' => 'frontend', 'store' => $store]);
            return $template;
        }

    } catch (\Exception $e) {
        $this->_logger->critical($e->getMessage());
        return;
    }
}    
}

Still not using my defined email template rather then still using the default email template from admin configuration, Is there anything I am missing here please guide about it.

Any help will be appreciated.

1 Answer 1

0

It is good and you followed good approach as per M2 standards. It needs some improvement to make it compatible and error-free in a maximum of cases

  1. Exception Handling. $order = $observer->getEvent()->getOrder(); if( !empty($order) ){ // do code here }

  2. Naming convention. Do use camel-case for variables. $order_status should be $orderStatus.

  3. It is observer so you won't be able to track if that happened or not so do wrap it in try-catch and log accordingly ( do add a check if logs are enabled or not ).
1
  • Thanks for the improvements you have mentioned, i have posted the least code here, Sure i will take and i am taking care of these standards, Can you please check the updated question now and try to answer that, will be appreciated.
    – Zoo
    Commented Apr 19, 2019 at 10:03

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.