Change Email Template On Specific Order Status:Magento2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Send Copy of Order Confirmation email programmatically in magento 2Set custom price of product when adding to cart code not workingMagento 2: How to override newsletter Subscriber modelMagento 2 Sending emails triggered by observerMagento 2: Add a product to the cart programmaticallyMagento 2: After custom cookie is created all pages default to home pageI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?Error Database Magento 2.3 migration from localhost to server

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Is a self contained air-bullet cartridge feasible?

RIP Packet Format

What helicopter has the most rotor blades?

Why isn't everyone flabbergasted about Bran's "gift"?

Co-worker works way more than he should

Why did Europeans not widely domesticate foxes?

What's the difference between using dependency injection with a container and using a service locator?

Philosophers who were composers?

Protagonist's race is hidden - should I reveal it?

Does using the Inspiration rules for character defects encourage My Guy Syndrome?

What was Apollo 13's "Little Jolt" after MECO?

`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY

France's Public Holidays' Puzzle

Why aren't road bicycle wheels tiny?

Coin Game with infinite paradox

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

Was Objective-C really a hindrance to Apple software development?

Are there existing rules/lore for MTG planeswalkers?

Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?

TV series episode where humans nuke aliens before decrypting their message that states they come in peace

My admission is revoked after accepting the admission offer

Israeli soda type drink



Change Email Template On Specific Order Status:Magento2



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Send Copy of Order Confirmation email programmatically in magento 2Set custom price of product when adding to cart code not workingMagento 2: How to override newsletter Subscriber modelMagento 2 Sending emails triggered by observerMagento 2: Add a product to the cart programmaticallyMagento 2: After custom cookie is created all pages default to home pageI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 Can't view module's front end page output?Error Database Magento 2.3 migration from localhost to server



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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="HawkOrderFlowObserverSalesOrderSalesOrderAfterSave"/>


And created an observer like follows:



<?php

namespace HawkOrderFlowObserverSalesOrder;

use MagentoFrameworkEventObserver as EventObserver;
use MagentoFrameworkEventObserverInterface;

class SalesOrderAfterSave implements ObserverInterface

/**
* @var MagentoFrameworkAppRequestHttp
*/
protected $_request;

/**
* @var MagentoFrameworkMailTemplateTransportBuilder
*/
protected $_transportBuilder;

/**
* @var MagentoStoreModelStoreManagerInterface
*/
protected $_storeManager;

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


public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkAppRequestHttp $request,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoStoreModelStoreManagerInterface $storeManager,
PsrLogLoggerInterface $logger
)
$this->_request = $request;
$this->_transportBuilder = $transportBuilder;
$this->_storeManager = $storeManager;
$this->_logger = $logger;


public function execute(MagentoFrameworkEventObserver $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.










share|improve this question






























    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="HawkOrderFlowObserverSalesOrderSalesOrderAfterSave"/>


    And created an observer like follows:



    <?php

    namespace HawkOrderFlowObserverSalesOrder;

    use MagentoFrameworkEventObserver as EventObserver;
    use MagentoFrameworkEventObserverInterface;

    class SalesOrderAfterSave implements ObserverInterface

    /**
    * @var MagentoFrameworkAppRequestHttp
    */
    protected $_request;

    /**
    * @var MagentoFrameworkMailTemplateTransportBuilder
    */
    protected $_transportBuilder;

    /**
    * @var MagentoStoreModelStoreManagerInterface
    */
    protected $_storeManager;

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


    public function __construct(
    MagentoFrameworkAppActionContext $context,
    MagentoFrameworkAppRequestHttp $request,
    MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
    MagentoStoreModelStoreManagerInterface $storeManager,
    PsrLogLoggerInterface $logger
    )
    $this->_request = $request;
    $this->_transportBuilder = $transportBuilder;
    $this->_storeManager = $storeManager;
    $this->_logger = $logger;


    public function execute(MagentoFrameworkEventObserver $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.










    share|improve this question


























      0












      0








      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="HawkOrderFlowObserverSalesOrderSalesOrderAfterSave"/>


      And created an observer like follows:



      <?php

      namespace HawkOrderFlowObserverSalesOrder;

      use MagentoFrameworkEventObserver as EventObserver;
      use MagentoFrameworkEventObserverInterface;

      class SalesOrderAfterSave implements ObserverInterface

      /**
      * @var MagentoFrameworkAppRequestHttp
      */
      protected $_request;

      /**
      * @var MagentoFrameworkMailTemplateTransportBuilder
      */
      protected $_transportBuilder;

      /**
      * @var MagentoStoreModelStoreManagerInterface
      */
      protected $_storeManager;

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


      public function __construct(
      MagentoFrameworkAppActionContext $context,
      MagentoFrameworkAppRequestHttp $request,
      MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
      MagentoStoreModelStoreManagerInterface $storeManager,
      PsrLogLoggerInterface $logger
      )
      $this->_request = $request;
      $this->_transportBuilder = $transportBuilder;
      $this->_storeManager = $storeManager;
      $this->_logger = $logger;


      public function execute(MagentoFrameworkEventObserver $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.










      share|improve this question
















      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="HawkOrderFlowObserverSalesOrderSalesOrderAfterSave"/>


      And created an observer like follows:



      <?php

      namespace HawkOrderFlowObserverSalesOrder;

      use MagentoFrameworkEventObserver as EventObserver;
      use MagentoFrameworkEventObserverInterface;

      class SalesOrderAfterSave implements ObserverInterface

      /**
      * @var MagentoFrameworkAppRequestHttp
      */
      protected $_request;

      /**
      * @var MagentoFrameworkMailTemplateTransportBuilder
      */
      protected $_transportBuilder;

      /**
      * @var MagentoStoreModelStoreManagerInterface
      */
      protected $_storeManager;

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


      public function __construct(
      MagentoFrameworkAppActionContext $context,
      MagentoFrameworkAppRequestHttp $request,
      MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
      MagentoStoreModelStoreManagerInterface $storeManager,
      PsrLogLoggerInterface $logger
      )
      $this->_request = $request;
      $this->_transportBuilder = $transportBuilder;
      $this->_storeManager = $storeManager;
      $this->_logger = $logger;


      public function execute(MagentoFrameworkEventObserver $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.







      magento2 php sales-order email-templates order-email






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 19 at 12:09







      Zoo

















      asked Apr 19 at 9:29









      ZooZoo

      136




      136




















          1 Answer
          1






          active

          oldest

          votes


















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





          share|improve this answer

























          • 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
            Apr 19 at 10:03












          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270773%2fchange-email-template-on-specific-order-statusmagento2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









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





          share|improve this answer

























          • 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
            Apr 19 at 10:03
















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





          share|improve this answer

























          • 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
            Apr 19 at 10:03














          0












          0








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





          share|improve this answer















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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 19 at 10:06









          Raj Mohan R

          708211




          708211










          answered Apr 19 at 9:51









          Shahzaib Hayat KhanShahzaib Hayat Khan

          29615




          29615












          • 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
            Apr 19 at 10:03


















          • 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
            Apr 19 at 10:03

















          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
          Apr 19 at 10:03






          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
          Apr 19 at 10:03


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270773%2fchange-email-template-on-specific-order-statusmagento2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sum ergo cogito? 1 nng

          三茅街道4182Guuntc Dn precexpngmageondP