Back to Top

How to use conditional (if) statement in email templates Magento2

Updated 18 December 2025

In this guide, we will learn how to apply conditional logic (if statements) in Magento 2 email templates. There are two common approaches to achieve this:

1. Using the depend Tag

The depend tag allows you to check whether a variable has a value before rendering content. You can include it in your email template like this:

{{depend info.getNotAllowDiscount()}}
    <h2 style="border-bottom:none;">{{trans 'Review Products'}}</h2>
{{/depend}}

The content inside the {{depend}}...{{/depend}} block will only be rendered if the condition evaluates to true.


2. Using a Custom Block

Another approach is to use a custom block in your email template. This is useful when you need more complex logic or want to include a separate PHTML template. For example:

{{block class='Magento\Framework\View\Element\Template' area='frontend' template='Vendor_Module::email/info.phtml' order=$order}}

Inside your custom PHTML template (info.phtml). You can write any PHP logic you need to conditionally display content.

Start your headless eCommerce
now.
Find out More

Example location:
vendor/magento/module-sales/view/frontend/email/shipment_new.html

If you have any questions, please comment below, and we will try to respond to you.

Thanks for visiting the Webkul blog! 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home