0

I declared a custom variable to New customer Email template,

{{var myvar1}}

I am assigning the value to that variable from my observer,

    <?php
    namespace test\test\Observer;

    use Magento\Framework\Event\ObserverInterface;

    class Customersubmit implements ObserverInterface
    {
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $customer = $observer->getEvent()->getCustomer();
            $customerId = $customer->getId();
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $transport = $objectManager->create('Magento\Framework\Mail\Template\Tran‌​sportBuilder');  

            if($customerId){
                $templateVars = array( 'myvar1' =>  'testing data' ); 
                $transport->setTemplateVars($templateVars); 
            }       
        }
    }

But I couldn't retrieve this value in my email . Could anyone say where I am wrong

3

2 Answers 2

1

You have to pass array as DataObject()

$transport = $objectManager->create('Magento\Framework\Mail\Template\Tran‌​sportBuilder');
$postObject = new \Magento\Framework\DataObject(array( 'myvar1' =>  'testing data' )); 
$transport->setTemplateVars(['customvar' => $postObject]);

In template file,

{{var customvar.myvar1}}
3
  • even after passing array as DataObject(), I couldn't retrieve the value.
    – Vigna S
    Mar 2, 2017 at 5:32
  • try with above code. Mar 2, 2017 at 5:35
  • I have tried,it dosen't helps. even I can't save the customer
    – Vigna S
    Mar 2, 2017 at 5:40
0

Put the variable which you want to set in Array first like :

$emailTempVar = array('name' => $cx_name,'email' => $cx_email);

then access it using :

$this->transportBuilder->setTemplateVars([
                        'email' => $emailTempVar['email'],
                        'name' => $emailTempVar['name']
                    ])

Then you can use it in email template like:

{{var name}} /{{var email}}

It works for me!

3
  • No , it's not working. I tried. Even I can't save the customer
    – Vigna S
    Mar 2, 2017 at 6:07
  • can you paste your observer's code here? Mar 2, 2017 at 6:10
  • I have pasted my observer
    – Vigna S
    Mar 2, 2017 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.