0

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

0

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"/>
    </event>
</config>

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

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

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

0

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])
                ->setTemplateVars([
                                 'store' => $this->storeManager->getStore(),
                                 'name'  => $name,
                                 'custom_data' => $data,
                                  ])
                ->setFrom('general') 
                ->addTo($email, $name)
                ->getTransport();
                $transport->sendMessage();

You can get these variables in email template by using

{{var name}}
{{var custom_data}}
1
  • 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.