4

I am new to Magento. I want to override new order email template and add custom details in order email. Please help.

Steps I have done

1.I have create new email template and assign to new order in from admin panel and section for custom details. it is working fine. But I don't know how to bind custom value

2. I have create observer after order success action and save custom data in sales_order against order id.

  public function execute(\Magento\Framework\Event\Observer $observer)
  {
   /* get customer store and restaurant data */
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $customerSession = $objectManager->get('Magento\Customer\Model\Session');

   $c_id = $customerSession->getCustomer()->getId();
   $c_store_id = $customerSession->getCustomer()->getData('stores');

   $store= $objectManager->get('Vendor\Store\Model\Template');
   $store_name = ($store->load($c_store_id)->getName());
   $store_restaurant_id = ($store->load($c_store_id)->getRestaurantId());

   $restaurant = $objectManager->get('Vendor\Restaurant\Model\Template');
   $restaurant_name = ($restaurant->load($store_restaurant_id)->getName());

    $orderids = $observer->getOrderIds();
    $orderId = ""; 

        foreach($orderids as $orderid){
            $order = $this->_order->load($orderid);
                $orderId = $orderid;
        }
    // Here I am saving custom data     
    $order->setData('restaurant_name', $restaurant_name )
            ->setData('res_store_name', $store_name);
  }
12

[Update] Add custom data in order email in Magento 2

At magento2 have an event which fire when order Prepare an email template with variables.

$this->eventManager->dispatch( 'email_order_set_template_vars_before', ['sender' => $this, 'transport' => $transport] );

So, at this event you can add new parameter via transport to template

Just like: events.xml

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_order_set_template_vars_before">
        <observer name="add_Custom_variable_to_Order" 
            instance="[Vendor]\[ModuleName]\Observer\ObserverforAddCustomVariable" />
    </event>
</config>

Observer file:

<?php
namespace [Vendor]\[ModuleName]\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverforAddCustomVariable implements ObserverInterface
{

    public function __construct(
    ) {
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $transport = $observer->getTransport();
        $transport['CustomVariable1'] = 'Your Value1 ';
        $transport['CustomVariable2'] = 'Your Value2';
    }
}

At the email template , you can get this custom variables CustomVariable1,CustomVariable2 using {{var CustomVariable1|raw}},{{var CustomVariable2|raw}}

Answer for save custom data in sales_order at success page

Magento fire an event when customer goes to order success page the event is :

checkout_onepage_controller_success_action

this event provide order id,So using this event you can save custom data in sales_order against order.

Also, in order save field you need to call repository \Magento\Sales\Api\OrderRepositoryInterface $orderRepositoryInterface and also add save() function for saving the data

   protected    $_orderRepositoryInterface ;
    public function __construct(
    ...
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepositoryInterface
    ....
    ) {
    .......
        $this->_orderRepositoryInterface = $orderRepositoryInterface;
    }


    public function execute(\Magento\Framework\Event\Observer $observer)
      {
       /* get customer store and restaurant data */
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $customerSession = $objectManager->get('Magento\Customer\Model\Session');

       $c_id = $customerSession->getCustomer()->getId();
       $c_store_id = $customerSession->getCustomer()->getData('stores');

       $store= $objectManager->get('Vendor\Store\Model\Template');
       $store_name = ($store->load($c_store_id)->getName());
       $store_restaurant_id = ($store->load($c_store_id)->getRestaurantId());

       $restaurant = $objectManager->get('Vendor\Restaurant\Model\Template');
       $restaurant_name = ($restaurant->load($store_restaurant_id)->getName());

        $orderids = $observer->getOrderIds();
        $orderId = ""; 

            foreach($orderids as $orderid){
                $order = $this->_orderRepositoryInterface->getById($orderid);
                $order->setData('restaurant_name', $restaurant_name )
                     ->setData('res_store_name', $store_name); 
                $order->save()             
            }


      }
  • Amit, I am able to save custom data in sales_order. My question is how to send these data in new order email to customer. – Abhishek Mar 12 '17 at 13:46
  • It works for me. But one thing here. I have to create events.xml in etc folder. Earlier it was under etc/frontend folder. – Abhishek Mar 15 '17 at 11:10
  • $transport is a \Magento\Framework\DataObject I don't think you can thread it like an array. More like $transport->setCustomVariable1('Hello'); – Andreas Riedmüller Oct 20 '17 at 15:15
  • I didn't get the custom variable value in order email templates. Observer is running fine. – Shekhar Suman Oct 31 '17 at 12:58
  • Observer can not call in order success. @Amit – Dhaduk Mitesh May 18 '18 at 7:05

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.