0

I am using magento 2.1 and i want to send the different registration emails or send different content, like separate login link for the customer belongs to different customer group. How can i achieve it. Please help.

1

First rewite below file vendor/magento/module-customer/Model/AccountManagement.php

Find below code from file

Function is :

  protected function sendNewAccountEmail(
    $customer,
    $type = self::NEW_ACCOUNT_EMAIL_REGISTERED,
    $backUrl = '',
    $storeId = '0',
    $sendemailStoreId = null
) {
    $types = $this->getTemplateTypes();

    if (!isset($types[$type])) {
        throw new LocalizedException(__('Please correct the transactional account email type.'));
    }

    if (!$storeId) {
        $storeId = $this->getWebsiteStoreId($customer, $sendemailStoreId);
    }

    $store = $this->storeManager->getStore($customer->getStoreId());

    $customerEmailData = $this->getFullCustomerObject($customer);

    $this->sendEmailTemplate(
        $customer,
        $types[$type],
        self::XML_PATH_REGISTER_EMAIL_IDENTITY,
        ['customer' => $customerEmailData, 'back_url' => $backUrl, 'store' => $store],
        $storeId
    );

    return $this;
}

and change

$this->sendEmailTemplate(
            $customer,
            $types[$type], ***// Your dynamic template goes here***
            self::XML_PATH_REGISTER_EMAIL_IDENTITY,
            ['customer' => $customerEmailData, 'back_url' => $backUrl, 'store' => $store],
            $storeId
        );
1

You can use {{if}} statements in email templates in order to check the value of variables.

This method is used in some Magento email templates, for example, for the logo height and width in the email header (module-email/view/frontend/email/header.html), or for displaying the address fields (configurations defined in module-customer/etc/config.xml).

{{if logo_width}}
    width="{{var logo_width}}"
{{else}}
    width="180"
{{/if}}

Unfortunately, as far as I know you can not use expressions. You can only check the variable value.

So, to solve you problem you could make a plugin before the object data is sent into the template and you could add one or more variables like this:

$customer->setData('is_in_customer_group_1', $customer->getCustomerGroup() == '1');
$customer->setData('is_in_customer_group_2', $customer->getCustomerGroup() == '2');
$customer->setData('is_in_customer_group_3', $customer->getCustomerGroup() == '3');

Then inside the template, in the places where you want to change the text, you can add an {{if}} condition for each group:

{{if customer.is_in_customer_group_1}}
...
{{/if}}
{{if customer.is_in_customer_group_2}}
...
{{/if}}
{{if customer.is_in_customer_group_3}}
...
{{/if}}
  • Any information about how we can make a plugin before the object data is sent into the template? – David Kooijman Jan 15 at 10:48
0

You need to rewrite Magento\Customer\Model\AccountManagement class

> <preference for="Magento\Customer\Model\AccountManagement"
> type="Company\Module\Model\Customer\AccountManagement" />
  • In in your aroundSendEmailConfirmation function get customer group and change the template file according to your need.
  • SendEmailConfirmation is a protected function, we can't plugin for it. – Nitin Pawar Aug 4 '17 at 9:59
  • Can you please share any other way to, implement this. I want to change the email text only based on the customer type. – Nitin Pawar Aug 4 '17 at 14:07
  • Right If the function is protected then plugin is not possible,can you try rewrite complete class through di preference method <preference for="Magento\Customer\Model\AccountManagement" type="Company\Module\Model\Customer\AccountManagement" /> – satya prakash patel Aug 5 '17 at 13:23

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.