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.