pass custom variable/parameter from email template to phtml file

pass custom variable/parameter from email template to phtml file

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
pass custom variable/parameter from email template to phtml file

Passing Custom Variables from Email Templates to PHTML Files in Magento

I understand your frustration with passing custom variables from email templates to phtml files in Magento. This is indeed a common issue faced by many developers while working on customizations. Let me guide you through the process step-by-step.

The Current Issue

You are currently trying to pass a custom variable test from the email template (app/locale/en_US/template/email/sales/order_new.html) to your phtml file (app/design/frontend/default/default/template/email/order/items/order/default.phtml). However, you’re not able to access this variable in your phtml file.

The Correct Approach

To achieve this, we need to make sure that the custom variable is correctly set in the email template and then properly accessed in the phtml file. Let’s go through the process:

Step 1: Set the Custom Variable in Order.php

You are already on the right track by setting the custom variable test in your sales_order model. However, it seems you’ve missed a small detail. The default code provided is properly setting the template parameters for the email.

The relevant part of your code is:

$mailer->setTemplateParams(array(
    'order'        => $this,
    'test'         => 'XXXXX',
    // ... other parameters
));

Here, test is correctly set as a parameter. The value 'XXXXX' can be replaced with whatever custom data you want to pass.

Step 2: Use the Custom Variable in the Email Template

Your email template (app/locale/en_US/template/email/sales/order_new.html) is also properly using the test variable within your layout handle:

{{layout handle="sales_email_order_items" order=$order test=$test}}

This will pass both the $order and $test variables to the template.

Step 3: Accessing the Custom Variable in the PHTML File

Now, let’s focus on accessing this variable in your phtml file (app/design/frontend/default/default/template/email/order/items/order/default.phtml).

You mentioned you’re trying to access it like this:

$test = $this->getItem()->getTest();

However, this is not the correct way. The getItem() method likely doesn’t have a property named test. Instead, we need to retrieve the passed variable from the template parameters.

In Magento 2, variables passed from the template are stored in $block->getData(). So, you should be able to access your custom variable like this:

$test = $this->getData('test');

This will properly fetch the value of the test variable that was passed from the email template.

Conclusion

Remember, when passing custom variables, always ensure they are correctly set in the model and accessed in the phtml file using $block->getData('variable_name'). This approach should solve your issue with accessing the test variable in your phtml file.

I encourage you to explore more about Magento 2’s email system for further customizations. Our blog at Laravel Company provides many tutorials and guides on Magento development that might be helpful for your future projects.

Don’t hesitate to reach out if you have any other questions!

Themes Configuration Email Orders

Our network