1

I want to print multiple coupon/gift codes in the custom email template. I am getting a single code as:

$templateVars = array(
    'store' => $this->storeManager->getStore(),
    'customer_name' => $name,
    'giftcods' => 'VFDFRVXSW43VF'
);

In HTML Template file:

<div style="color:#696969;font-size:20px;text-align:center;">
    <strong>{{var giftcods|raw}}</strong>
</div>

And output as:

enter image description here

But this is a single code, I won't to print multiple codes in an email. I have gone by some related links like:

Magento 2 : How to handle array values in custom email templates?

Pass Variable And Output In Custom Email Template

But there is no proper solution available specially about how to deal with array value in the HTML template.

Edit:

In vendor/magento/module-gift-card Module this is done as

In file: vendor/magento/module-gift-card/view/frontend/email/gift_information.html

{{if is_multiple_codes}}
<h3>{{trans "Gift Card Codes:"}}</h3>
{{else}}
<h3>{{trans "Gift Card Code:"}}</h3>
{{/if}}

<p>{{var giftcards|raw}}</p>

Variables are sending by Magento\GiftCard\Model\GiftCardItemEmail.php as:

$templateData = [
    'name' => $giftCardOrderItem->getProductOptionByCode('giftcard_recipient_name'),
    'sender_name' => $senderName,
    'giftcards' => $codeList->toHtml(),
    'balance' => $balance,
    'is_multiple_codes' => 1 < $generatedCodesCount,
    'store' => $giftCardOrderItem->getStore(),
    ..................
    ..................
];

Here $codeList is not array. But still looking to solve!

7
  • Pass multiple code in giftcods as array. Nov 12, 2019 at 12:41
  • Use 'giftcods' => ['VFDFRVXSW43VF', 'code 2', 'code 3']. Nov 12, 2019 at 12:47
  • By this I get error when clicking on email to open in inbox: Error filtering template: Notice: Array to string conversion in /var/www/html/vendor/magento/framework/Filter/Template.php on line 173
    – Ajwad Syed
    Nov 12, 2019 at 12:53
  • Use 'giftcods' => ['key1' => 'VFDFRVXSW43VF', 'key2' => 'code 2', 'key3' => 'code 3']. Nov 12, 2019 at 12:56
  • Same :( @DhadukMitesh
    – Ajwad Syed
    Nov 12, 2019 at 12:59

1 Answer 1

2

Please add this to the PHP file.

$giftcods = ['VFDFRVXSW43VF', 'FFRFRFVSW43VF', 'VFDFGFGFXSW43VF'];
$templateVars = [
    'store' => $this->storeManager->getStore(),
    'customer_name' => $name,
    'giftcods' => implode("<br>",$giftcods) 
];

And set in template as ->setTemplateVars($templateVars)

Use variable in HTML as,

<div style="color:#696969;font-size:20px;text-align:center;"> 
    <strong>{{var giftcods|raw}}</strong> 
</div>

Clear cache: php bin/magento cache:clean and check it.

2
  • I can get codes one by one with this.. for first {{var templateVars.giftcods.code1|raw}}, for secound {{var templateVars.giftcods.code2|raw}} and so on.. I need to print all the codes in array at once!
    – Ajwad Syed
    Nov 13, 2019 at 7:11
  • above code should work. What's issue are you facing?
    – Khushbu
    Nov 13, 2019 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.