Change Email Template On Specific Order Status:Magento2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm 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

Improvising over quartal voicings

How many time has Arya actually used Needle?

How is the policy gradient calculated in REINFORCE?

One-one communication

How do you cope with tons of web fonts when copying and pasting from web pages?

"Destructive power" carried by a B-52?

How do Java 8 default methods hеlp with lambdas?

How to infer difference of population proportion between two groups when proportion is small?

Twin's vs. Twins'

calculator's angle answer for trig ratios that can work in more than 1 quadrant on the unit circle

How do I find my Spellcasting Ability for my D&D character?

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

Keep at all times, the minus sign above aligned with minus sign below

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

How to achieve cat-like agility?

3D Masyu - A Die

How can I list files in reverse time order by a command and pass them as arguments to another command?

Getting representations of the Lie group out of representations of its Lie algebra

Where did Ptolemy compare the Earth to the distance of fixed stars?

Is there night in Alpha Complex?

How to name indistinguishable henchmen in a screenplay?

NIntegrate on a solution of a matrix ODE

Can gravitational waves pass through a black hole?

Can I cut the hair of a conjured korred with a blade made of precious material to harvest that material from the korred?



Change Email Template On Specific Order Status:Magento2



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm 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 2 days ago







      Zoo

















      asked 2 days ago









      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
            2 days ago












          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
            2 days ago
















          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
            2 days ago














          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 2 days ago









          Raj Mohan R

          673110




          673110










          answered 2 days ago









          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
            2 days ago


















          • 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
            2 days ago

















          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
          2 days ago






          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
          2 days ago


















          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

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

          Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?