Is their any way we can pass some static data to order confirmation email template ?

And then use it as variable in email template ?

like {{var order.pickupDate}}

Thank you


You can add using Observer.

Create events.xml file on app/code/Vendor/Module/etc

<?xml version="1.0" ?>
<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 instance="Vendor\Module\Observer\Email\OrderSetTemplateVarsBefore" name="v4u_emailvar_observer_email_ordersettemplatevarsbefore_email_order_set_template_vars_before"/>

Create OrderSetTemplateVarsBefore.php on app/code/Vendor/Module/Observer/Email

 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.

namespace Vendor\Module\Observer\Email;

use Magento\Customer\Api\CustomerRepositoryInterface;

class OrderSetTemplateVarsBefore implements \Magento\Framework\Event\ObserverInterface

    protected $customerRepository;

    public function __construct(
        CustomerRepositoryInterface $customerRepository
    ) {
        $this->customerRepository = $customerRepository;

     * Execute observer
     * @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->getEvent()->getTransport();
        if($transport->getOrder() != null)
            $customer = $this->customerRepository->getById($transport->getOrder()->getCustomerId());
            $transport['userId'] = $customer->getId();

As per Our observer defined You can use

{{trans 'User Id : '}}{{var userId}}

For more info you can review here


You can use trasnportbuilder for the same. You can paas your custom data using the SetTemplateVars

$transport = $this->transportBuilder->setTemplateIdentifier($templateId, 'template_id')
                ->setTemplateOptions(['area' => 'frontend', 'store' => $store_id])
                                 'store' => $this->storeManager->getStore(),
                                 'name'  => $name,
                                 'custom_data' => $data,
                ->addTo($email, $name)

You can get these variables in email template by using

{{var name}}
{{var custom_data}}
  • Please check if it works for you. Feb 12 at 8:18

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.