Magento - how to add custom variables to new order E-mail?

Magento - how to add custom variables to new order E-mail?

July 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - how to add custom variables to new order E-mail?

Adding Custom Variables to Magento New Order Email

Adding custom variables to your Magento new order email is an essential step for enhancing customer communication and providing more relevant information about their orders.

Understanding Magento’s Email Template System

Magento uses its built-in template system to generate emails for various events like new orders, invoice generation, etc. These templates are located in the app/design/frontend/<package>/<theme>/template/email directory.

The default ‘sales_email_order_new’ template is responsible for sending the new order email.

Retrieving Custom Customer Attributes

To include custom customer attributes in your email, you first need to retrieve these values from the database. Magento stores custom customer attributes in the customer_entity_varchar and customer_entity_text tables.

Let’s assume you have a custom attribute named ‘loyalty_points’ stored as an integer.

$customerId = 1; // Replace with actual customer ID

$attributeCode = 'loyalty_points';

$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "SELECT value FROM {$this->getTable('customer_entity_int')} WHERE entity_id=".$customerId." AND attribute_id=(SELECT attribute_id FROM {$this->getTable('eav_attribute')} WHERE attribute_code=".$attributeCode.")";
$res = $connection->fetchOne($query);

echo 'Loyalty Points: '.$res;

This example fetches the value of the custom ‘loyalty_points’ attribute for a specific customer.

Modifying the Email Template

Now, you need to modify the email template to include this custom variable. Open the sales_email_order_new.html file located in your theme directory (usually under app/design/frontend/<package>/<theme>/template/email).

Find the section of the template where you want to insert the new data. For example, if you want to display ‘Loyalty Points’ above the order summary:

<!-- Message about order status -->
<p><strong>{{var store.getName()}} - Order # {{var increment_id}}</strong></p>

<!-- Custom Variable -->
<p>Your current loyalty points: {{var customer.loyalty_points}}</p>

<!-- Order Summary -->
...

In the above code, {{var customer.loyalty_points}} is a placeholder that will be replaced by the actual value of ‘loyalty_points’ attribute for the specific customer.

Note: This assumes you have set up Magento’s custom variable system correctly and have mapped this attribute to the template using the appropriate XML configuration in your theme.

Testing Your Changes

After making these changes, save the file and clear your cache in Magento (System > Cache Management).

You can then create a test order or simulate an order event using tools like MageFlow or MageTest.

Conclusion

Adding custom variables to Magento’s new order email is a straightforward process that involves retrieving data from customer attributes and incorporating it into your email template. By following the steps outlined above, you can enhance your emails with relevant customer-specific data without having to modify core files.

Remember to always backup your templates before making changes, and test thoroughly after implementation to ensure no formatting or functionality issues arise.

For more in-depth guides on Magento customization and best practices, visit Laravel Company for expert insights and tips.

Database Themes Email Orders

Our network