2

In magento 2 it is possible to create Email Template variables, however, those variables are html or plain text.

I'd like to create a variable containing the user group, so i can use this variable to show different text for different user groups.

Thanks in advance!

  • can you specify the templates where you want to use it? because you can't do comparisions, but you can check if a variable is set or not. – Philipp Sander Jan 22 at 8:55
  • I would love if I could do some sort of check if a variable is set. Is it possible to write content that would be only shown to certain user groups? The only thing i want is that you have content block a in a mailing template for user group a, and other content block in a mailing template for user group b – David Kooijman Jan 22 at 16:05
1
+50

If you want to do a comparisons like this: {{if customer.getGroupId()==2}}Group 2{{else}})Group 1{{/if}} than you'll need to rewrite this class: \Magento\Framework\Filter\Template which is the equivalent of Magento 1's lib/Varien/Filter/Template.php. This class did not change much for years. It's been like this since Magento 1 was released. You can add your preference in di.xml for \Magento\Framework\Filter\Template. It seems that there were some edits in Magento 2.3.x. They introduced a 'loop' directive.

I created these snippets. https://gist.github.com/daniel-ifrim/74a0310c6acd9f664381c895bf0fcb35 The code should be maintained on Magento 2 upgrades. Basically compare the filter method and the constants took from the original class.

A Magento 1 code snippet. Originally was tested for customer address summary. With little more code (class rewrite) it should work with emails too. https://gist.github.com/daniel-ifrim/420cfffc28a4c0ca29b7e3c4a18a98a6

  • you can't do comparisons in templates. you can only check if a variable is set to a value other than false or null – Philipp Sander Jan 22 at 10:21
  • You can if you rewrite \Magento\Framework\Filter\Template and/or Magento\Email\Model\Template\Filter class and make it work. You can't do it by default. I did it already in a Magento 1 extension and in Magento 2. The author of the question asked for a solution. He didn't asked what can be done by default in Magento. – obscure Jan 22 at 10:39
  • then you should link to the extension if it can't be done in vanilla magento – Philipp Sander Jan 22 at 10:40
  • This is what i'm looking for. Would you be able to send me code examples for an extension that does these things? – David Kooijman Jan 22 at 16:06
  • @DavidKooijman Hi, I will try to make a small module on Github. Until than I'll post it on Gist – obscure Jan 22 at 19:24
0

Since you want specific content for each user group, simply create multiple Email Template with different contents. Then While sending Email using TransportBuilder, just change the templates based on customer user groups.

 $transport = $this->_transportBuilder
        ->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
        ->setTemplateOptions(
            [
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file
                'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ]
        )
        ->setTemplateVars(['data' => $postObject])
        ->setFrom($sender)
        ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
        ->getTransport();

Check conditons based on user group and change the template Identifier.

if(userGroup == 'a'){
setTemplateIdentifier('send_email_email_template')
}elseif (userGroup == 'b'){
setTemplateIdentifier('some_other_email_template')
}
-1

Overwrite vendor/magento/module-customer/Model/AccountManagement.php

<preference for="Magento\Customer\Api\AccountManagementInterface"
                type="Mynamespace\Mymodule\Model\AccountManagement" />

check function sendNewAccountEmail()

set the data in $customerEmailData and get into the email template.

  • thanks for your help, but this core editing and i dont want that :D – David Kooijman Jan 22 at 16:05
  • @DavidKooijman, you can override the file in your module using preference. – Amit Naraniwal Jan 23 at 4:56

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.