Magento If else statement in transactional emails - surely this is simple?

Magento If else statement in transactional emails - surely this is simple?

September 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento If else statement in transactional emails - surely this is simple?

Solving Magento Transactional Email Logic Issues: A Comprehensive Guide

As a senior developer, I often encounter questions like this - seemingly simple tasks that can trip up even experienced Magento developers. Let’s dive into this transactional email logic issue and resolve it step-by-step.

The Challenge: Conditional Logic in Transactional Emails

You’re trying to insert conditional logic into your invoice transactional email template, specifically for the payment method field. Your goal is to display different messages based on whether the payment method is ‘Payment after delivery’ or not.

The issue lies in the fact that Magento’s email templates are rendered using PHP and Smarty, which have slightly different syntax compared to pure PHP. This can lead to confusion when trying to insert if-else statements directly into the template file.

Let’s Break It Down

Firstly, let me clarify your current situation:

  1. You have an invoice transactional email that displays a payment method field.
  2. The existing code simply sets this field to “Payment has been received”.
  3. However, you want to display different messages based on the actual payment method used.

The Solution: Using Magento’s Template Variables

The key is to leverage Magento’s template variables and a bit of Smarty syntax magic to achieve your desired functionality.

Here’s how we can modify your existing code:

<td class="method-info">
  <h6>Payment method:</h6>
  {if $payment_html eq 'Payment after delivery'}
    {$payment_html}
  {else}
    Payment has been received
  {/if}
</td>

Let me explain the modifications:

  • We’re using Smarty’s {if} statement, which works similarly to PHP’s if statement.
  • $payment_html is a variable that already exists in the Magento template context. This contains the actual payment method description.
  • The {if $payment_html eq 'Payment after delivery'} line checks if this variable exactly matches the string Payment after delivery.
  • If it does, we simply print the value of $payment_html using {$payment_html}.
  • If not, we display the static text “Payment has been received”.
  • The {/if} statement closes the if block.

A Word of Caution

While this solution works perfectly fine for your specific use case, it’s important to note a few things:

  1. Variable Names: Always double-check the actual variable names used in your template file. In some cases, Magento may use different variable names than you expect.
  2. Escaping Output: If the output of $payment_html contains any special characters or HTML tags, you might need to use Smarty’s {escape} function to ensure proper rendering.

Conclusion

As we’ve seen, solving this issue is more about understanding Magento’s template context and using the appropriate Smarty syntax than it is about raw PHP coding. By leveraging existing variables and applying basic conditional logic, we can achieve the desired functionality in our transactional email templates without needing to write any complex custom code.

Remember, properly maintaining your email templates is crucial for ensuring accurate communication with your customers. If you find yourself struggling with Magento development tasks like this regularly, consider reaching out to a professional agency or consultant for expert guidance and support.

For more in-depth tutorials on Magento 2 development practices, feel free to explore our blog at https://laravelcompany.com/blog. We provide regular updates on best practices, tips, and tricks to help you master the platform. Happy coding!

Themes Email Orders Pricing

Our network