How to loop through a variables in Magento Email Template?

How to loop through a variables in Magento Email Template?

August 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to loop through a variables in Magento Email Template?

Looping Through Variables in Magento Email Template

Email templates in Magento provide a powerful way to customize the content sent to customers. However, sometimes you may need to loop through complex data structures like collections within your email template. In this guide, we’ll explore how to iterate over a collection of products and display them in an email.

Understanding Your Data Structure

First, let’s understand the data structure we’re working with:

$vars = array(
    'products'  => $products,
);

Here, $products is assumed to be a Magento product collection retrieved using the Mage::getModel('catalog/product')->getCollection() method or similar.

Iterating Over the Collection

Magento email templates use a specific syntax for looping through arrays. To iterate over your products collection in the email template, you can use the following code:

{{if products}}
   <table>
       {{foreach from=$products item="product"}}
           <tr>
               <td>{{product.name}}</td>
               <td>$${{product.price}}</td>
           </tr>
       {{/foreach}}
   </table>
{{/if}}

Let’s break down this code:

  • {{if products}} - This line checks if the ‘products’ variable exists and has content. It wraps the entire loop structure.
  • {{foreach from=$products item="product"}} - The foreach statement initiates a loop through each product in the collection. The item="product" part assigns the current element as a local variable named $product.
  • {{product.name}} and $$${{product.price}} - These are syntax for accessing variables within the email template. {{product.name}} will display the name of the current product, while $$${{product.price}} will format the price (adding a dollar sign).
  • {{/foreach}} - This line closes the foreach loop.
  • {{/if}} - It closes the if statement that checks for the existence of products.

Displaying Additional Product Details

You can access other product attributes as well by replacing name and price with your desired attribute code. For example, to display SKU:

<td>{{product.sku}}</td>

Conclusion: Looping for Enhanced Email Content

Looping through collections in Magento email templates allows you to create dynamic content that adapts to the data sent from your store. This is particularly useful when sending order confirmations, order updates, or notifications with multiple items.

Remember to always validate and sanitize your variables to prevent any potential security vulnerabilities. Also, consider using Magento’s built-in layout handles for a cleaner separation of concerns between your template logic and content.

For more advanced email customization techniques in Magento, including conditional statements and complex data manipulations, feel free to explore our Guide to Extending Magento Email Templates at Laravel Company.

Catalog Database Themes Email

Our network