Magento – Insert custom variable into email templates in magento 2

customer-addressemail-templatesmagento2.2

I'm trying to insert custom variable(company, Phone Number) into my customer email template. But it's not working for me.

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

    <p class="greeting">{{trans "%name," name=$customer.name}}</p>
    <p>{{trans "Welcome to %store_name." 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>
    <table class="email-credentials">
        <tr>
            <th>{{trans "Email:"}}</th>
            <td>{{var customer.email}}</td>
        </tr>
        <tr>
            <th>{{trans "Password:"}}</th>
            <td><em>{{trans "Password you set when creating account"}}</em></td>
        </tr>
    </table>
    <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>
    <br />
    <p>
    {{var company}}
    </p>
    <br/>
    <p>{{trans "When you sign in to your account, you will be able to:"}}</p>
    <table class="email-features">
        <tr>
            <td>
                <table>
                    <tr>
                        <td>
                            <img src="{{view url='Magento_Customer/images/icn_checkout.png'}}" height="30" width="30" alt="{{trans 'Quick Checkout'}}" />
                        </td>
                        <td>
                            <h3>{{trans "Proceed through checkout faster"}}</h3>
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <table>
                    <tr>
                        <td>
                            <img src="{{view url='Magento_Customer/images/icn_status.png'}}" height="30" width="30" alt="{{trans 'Order Status'}}" />
                        </td>
                        <td>
                            <h3>{{trans "Check the status of orders"}}</h3>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td>
                            <img src="{{view url='Magento_Customer/images/icn_address.png'}}" height="30" width="30" alt="{{trans 'Manage Addresses'}}" />
                        </td>
                        <td>
                            <h3>{{trans "Store alternative addresses"}}</h3>
                            <p>{{trans "For shipping to multiple family members and friends"}}</p>
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <table>
                    <tr>
                        <td>
                            <img src="{{view url='Magento_Customer/images/icn_history.png'}}" height="30" width="30" alt="{{trans 'Order History'}}" />
                        </td>
                        <td>
                            <h3>{{trans "View past orders"}}</h3>
                        </td>
                    </tr>
                </table>
            </td>
    </table>
    <br/><br/>

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

Any help on this?

Best Answer

Try with:

{{var customer.company}} and {{var customer.phone_number}}

If it's not working, then the customer object for email templates doesn't contain the newly added customer data (company and phone number).

You can check the function newAccount() in \vendor\magento\module-customer\Model\EmailNotification.php which contains $customerEmailData that holds all customer related data for the email template.

You could create a plugin or override to add your custom data to it.