TransWikia.com

Magento 2 : Use Customer Data Model in transactional e-mail templates

Magento Asked by Giel Berkers on February 2, 2021

I’m trying to send a transactional mail and I want to use the customer data model in my e-mail template. Note: Customer Data Model (MagentoCustomerModelDataCustomer). Not Customer Model (MagentoCustomerModelCustomer).

This is my transport builder:

// Build message:
$transport = $this->transportBuilder
    ->setTemplateIdentifier('reseller_notification_mail')
    ->setTemplateVars([
        'subject'     => __('New Reseller subscription from %1.',
            $this->configHelper->get('general/store_information/name')),
        'customer'    => $customer,
        'reseller_id' => $customer->getCustomAttribute(CustomerHelper::ATTRIBUTE_RESELLER_ID)->getValue()
    ])
    ->setTemplateOptions([
        'area'  => 'frontend',
        'store' => MagentoStoreModelStore::DEFAULT_STORE_ID,
    ])
    ->setFrom($identity)
    ->addTo($this->configHelper->get("trans_email/ident_{$identity}/email"))
    ->getTransport();

// Send message:
$transport->sendMessage();

And this is my e-mail template:

<!--@subject {{var subject}} @-->
<!--@vars {
"var customer":"Customer",
"var customer.getFirstName()","Customer First Name",
"var customer.getLastName()","Customer Last Name",
"var customer.getEmail()","Customer E-mail",
"var reseller_id":"Reseller ID"
} @-->

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

<p>There has been a new reseller subscription on the website:</p>

<table>
    <tr>
        <th>{{trans "Name"}}</th>
        <td>
            {{var customer.getFirstName()}} {{var customer.getLastName()}}
        </td>
    </tr>
    <tr>
        <th>{{trans "E-Mail Address"}}</th>
        <td>{{var customer.getEmail()}}</td>
    </tr>
    <tr>
        <th>{{trans "Reseller ID"}}</th>
        <td>{{var reseller_id}}</td>
    </tr>
</table>

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

However, in the e-mail I keep getting the following error:

Error filtering template: 
Warning: htmlspecialchars() expects parameter 1 to be string, object given in 
/var/www/public/vendor/magento/module-email/Model/Template/Filter.php on line 691

I know the problem has to be in the way I’m trying to read the name and e-mail address of my customer. Because if I remove those lines from the template, there is no error message and the rest of the mail is parsed accordingly.

I’ve already tried a couple of things:

  • Wrap the customer inside a data object
  • Prepend customer in my template with a dollar sign ($customer.getFirstName())
  • Provide an array with customer data and just use them as a string ({{var data.customer.first_name}}), but then the error is ‘expects string, an array is given ‘.

Does anyone know what I’m doing wrong here? I know it must be something small I’m overseeing here but I can’t figure it out. I’m using Magento 2.1.3.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP