Is there a way to add foreach loop in email template?
I think that it is not possible to directly perform a foreach loop in emails, but what you could do is include a block that will do the loop in a regular template.
In you email add something like the following.
{{block type='core/template' area='frontend' template='email/template.phtml' items=$items}}
Then in your template add the following.
<?php foreach ($this->getItems() as $_item): ?>
<p><?php echo $_item['name'] ?></p>
<?php endforeach; ?>
For more information I followed this blog
Actually you can use a for loop in emails. I've tested it on 2.3.3 version. This is the code location to inspect. This is how it looks like in the template:
<ul>
{{for item in data.items}}
<li>Name: {{var item.name}}</li>
{{/for}}
</ul>
The transport information:
$this->transportBuilder->setTemplateVars(
[
'items' => [
['name' => 'Product1'],
['name' => 'Product2']
]
...
Aside. In 2.3.4 the code was refactored, but I believe it still should work as it was not removed. Please remember that Magento removed the ability to call functions on objects as of this version, so your order->getItems()
function may not work.