Add Variables to the Magento Emails

This article will demonstrate how to add variables to the Magento emails.

The example below will focus on adding the customer group into the order email for registered users template.

To start with, let’s take a look where Magento sends out the order email. This happens within the Mage_Sales_Model_Order class within the queueNewOrderEmail() method.

As of Magento Community/Open Source version 1.9.1, all emails are queued and sent through your configured cron schedule.

<?php
class Mage_Sales_Model_Order extends Mage_Sales_Model_Abstract
{
    ....

    public function queueNewOrderEmail($forceMode = false)
    {
        ....

        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
            'order'        => $this,
            'billing'      => $this->getBillingAddress(),
            'payment_html' => $paymentBlockHtml
        ));

        /** @var $emailQueue Mage_Core_Model_Email_Queue */
        $emailQueue = Mage::getModel('core/email_queue');
        $emailQueue->setEntityId($this->getId())
            ->setEntityType(self::ENTITY)
            ->setEventType(self::EMAIL_EVENT_NAME_NEW_ORDER)
            ->setIsForceCheck(!$forceMode);

        $mailer->setQueue($emailQueue)->send();

        $this->setEmailSent(true);
        $this->_getResource()->saveAttribute($this, 'email_sent');

        return $this;
    }
}

Variables are passed to the email template using the setTemplateParams() method of the Mage_Core_Model_Email_Template_Mailer class.

These variables can also be seen within the admin when attempting to load the transactional email template in the System -> Transactional Emails section.

Add Variables to the Magento Emails

So in order for our own custom group variable to this method, we’ll need to rewrite the Mage_Sales_Model_Order class. To do this a custom module will need to be added.

Start with the module’s declaration file.

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Orderemail>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Sales />
            </depends>
        </Custom_Orderemail>
    </modules>
</config>

Now add a config.xml file. This configuration file will specify the rewrite.

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <sales>
                <rewrite>
                    <order>Custom_Orderemail_Model_Order</order>
                </rewrite>
            </sales>
        </models>
    </global>
</config>

Add the model class ensuring that the class extends Mage_Sales_Model_Order. Take a copy of the original queueNewOrderEmail() method and paste it into your class and then add the variable within the setTemplateParams() array.

<?php
class Custom_Orderemail_Model_Order extends Mage_Sales_Model_Order
{
    public function queueNewOrderEmail($forceMode = false)
    {
        ....

        // Set all required params and send emails
        $mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
            'order'          => $this,
            'billing'        => $this->getBillingAddress(),
            'payment_html'   => $paymentBlockHtml,
            'customer_group' => $this->_getCustomerGroup()
        ));

        /** @var $emailQueue Mage_Core_Model_Email_Queue */
        $emailQueue = Mage::getModel('core/email_queue');
        $emailQueue->setEntityId($this->getId())
            ->setEntityType(self::ENTITY)
            ->setEventType(self::EMAIL_EVENT_NAME_NEW_ORDER)
            ->setIsForceCheck(!$forceMode);

        $mailer->setQueue($emailQueue)->send();

        $this->setEmailSent(true);
        $this->_getResource()->saveAttribute($this, 'email_sent');

        return $this;
    }
}

The customer_group variable is retrieved from a _getCustomerGroup() method added within our custom class. The conditional statement checks if the customer has ordered as a guest and if so, returns N/A. Otherwise, the customer group name is returned.

<?php
class Custom_Orderemail_Model_Order extends Mage_Sales_Model_Order
{
    ....
  
    protected function _getCustomerGroup()
    {
        $group = Mage::getModel('customer/group')->load($this->getCustomerGroupId());
        return $group->getCode();
    }

Now in order to print out our new variable within the email template HTML itself, we shouldn’t edit the email template file directly.

Instead, load the New Order for Registered Users email template within System -> Transactional emails. You’ll then be able to add the customer_group variable into the template content area.

Add Variables to the Magento Emails

Don’t forget to change the email template that is used in the Sales Emails section to the template saved within System -> Transactional Emails.

Add Variables to the Magento Emails

Now when registered customers make an order through your store, the email template sent out should contain your customer group variable with the relevant customer group name returned.

Add Variables to the Magento Emails

Note: This article is based on Magento Community/Open Source version 1.9.