Magento – Magento 2: Add custom variable to email template

email-templatesmagento-2.1magento2

I added a "regular card number" attribute to the customer. It is essentially a unique code that when submitted by the customer gives a 20% discount. I would now like to send that code in an email to the customer, but Magento doesn't seem to add it.

<li>{{trans "Regular card number: %card_number" card_number=$customer.getCardNumber()}} {{var customer.custom('card_number')}}</li>

Any ideas on how to add this variable to an email template?

Thank you!


Full code of my template:

<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<!--@subject {{trans "Welcome to %store_name" store_name=$store.getFrontendName()}} @-->
<!--@vars {
"var this.getUrl($store, 'customer/account/')":"Customer Account URL",
"var customer.email":"Customer Email",
"var customer.name":"Customer Name",
} @-->
{{template config_path="design/email/header_template"}}

<p class="greeting">{{trans "%name," name=$customer.name}}</p>
<p>{{trans "Thank you for confirming your %store_name account." store_name=$store.getFrontendName()}}</p>
<p>
    {{trans
        'To sign in to our site, use these credentials during checkout or on the <a href="%customer_url">My Account</a> page:'

        customer_url=$this.getUrl($store,'customer/account/',[_nosid:1])
    |raw}}
</p>
<ul>
    <li><strong>{{trans "Email:"}}</strong> {{var customer.email}}</li>
    <li><strong>{{trans "Password:"}}</strong> <em>{{trans "Password you set when creating account"}}</em></li>
    <li>{{trans "Regular card number: %card_number" card_number=$customer.getCardNumber()}} {{var customer.custom('card_number')}}</li>
</ul>
<p>
    {{trans
        'Forgot your account password? Click <a href="%reset_url">here</a> to reset it.'

        reset_url="$this.getUrl($store,'customer/account/createPassword/',[_query:[id:$customer.id,token:$customer.rp_token],_nosid:1])"
    |raw}}
</p>
<p>{{trans "When you sign in to your account, you will be able to:"}}</p>
<ul>
    <li>{{trans "Proceed through checkout faster"}}</li>/
    <li>{{trans "Check the status of orders"}}</li>
    <li>{{trans "View past orders"}}</li>
    <li>{{trans "Store alternative addresses (for shipping to multiple family members and friends)"}}</li>
</ul>

{{template config_path="design/email/footer_template"}}

Best Answer

First, i think you have to check if in Magento\Customer\Model\EmailNotification in the method newAccount() the variable $customerEmailData contains your card_number values.

Then if it is present you can retrieve it in template with

<p><strong>{{trans "Regular card number:"}}</strong> {{var customer.card_number}}</p>

Instead if it is not present you have to add it to template params (see the sendEmailTemplate() method).

Now the newAccount() method hasn't a dispatched event, so you can't use event/observer, you can try with a plugin, even if i think that in this case the safest ways is to override the class and the method with a custom class.