I want add custom attribute value in new order email template.
Please help me in this.
email-templatesmagento2order-email
I want add custom attribute value in new order email template.
Please help me in this.
Once you created email template you can get with below default code of magento source model
<notification>
<label>Select Email Template </label>
<comment>It is advisable to create a new email template for this purpose and then select it in this configuration. Make sure you add review URL in your template. For e.g., {{ReviewURL}} <![CDATA[ <br /><span style="color:#FFA500">NOTE:- Default template from locale should not be used. Make sure you add product Name in your template. For e.g., {{ProductName}} You can also use other variables like OrderNumber {{OrderNumber}}</span>]]></comment>
<frontend_type>select</frontend_type>
<backend_model>mcautoreviewreminderemail/Mcautoreviewreminderemailtemplate</backend_model>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<can_be_empty>1</can_be_empty>
<notification>
i am sure it will work for you.
use transportObject Use transportObject
instead of transport
$this->eventManager->dispatch(
'email_order_set_template_vars_before',
['sender' => $this, 'transport' => $transportObject, 'transportObject' => $transportObject]
);
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}}
Magento fire an event when a customer goes to order success page the event is :
checkout_onepage_controller_success_action
this event provides 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()
}
}
Best Answer
If the name of the attribute is the following:
Just modify the following template:
with copying to:
and insert the following statement to show the variable in the email:
tested with M2.3.4