Transactional Email – Adding Custom Function Output

Having the basics of Transactional Emails under one’s belt it is then safe to start taking things to the next level.

Consider the case of wanting to implement a new function that is currently not part of the basic order Transactional Email list.

In this case it is possible to rewrite the order model and implement your own functionality.

The creation of the necessary skeleton files are as follows:

/etc/module/{Namespace}_{Module}.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
    <modules>
        <{Namespace}_{Module}>
            <active>true</active>
            <codePool>local</codePool>
        </{Namespace}_{Module}>
    </modules>
</config>

/app/code/local/{Namespace}/{Module}/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <{Namespace}_{Module}>
            <version>0.1.0</version>
        </{Namespace}_{Module}>
    </modules>

    <global>
        <models>
           <core>
                <rewrite>
                    <email_template>{Namespace}_{Module}_Model_Email_Template</email_template>
                </rewrite>
            </core>
            <sales>
                <rewrite>
                    <order>{Namespace}_{Module}_Model_Email_Order</order>
                </rewrite>
            </sales>

            <emails>
                <class>{Namespace}_{Module}_Model</class>
            </emails>
        </models>

        <!-- Adds an entry into Transactional Email -->
        <template>
            <email>
                <!-- This value should reflect the path defined in the system.xml file: customer - order_confirmation_email - confirmation_template -->
                <customer_order_confirmation_email_confirmation_template translate="label" module="customer">
                    <label>Custom Order Confirmation</label>
                    <!-- Create file in the language locations at /app/locale/<language code>/email/order/confirmation.hmtl -->
                    <file>order/confirmation.html</file>
                    <type>html</type>
                </customer_order_confirmation_email_confirmation_template>
            </email>
        </template>
    </global>
</config>

/app/code/local/{Namespace}/{Module}/Model/Email/Order.php

<?php
class {Namespace}_{Module}_Model_Email_Order extends Mage_Sales_Model_Order {

	public function customFunction() {
		return 'This is a custom function return string';
	}
}

/app/code/local/{Namespace}/{Module}/Model/Email/Template.php

<?php
class {Namespace}_{Module}_Email_Template extends Mage_Core_Model_Email_Template {

	public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)
	{
		$this->setSentSuccess(false);
		if (($storeId === null) && $this->getDesignConfig()->getStore()) {
			$storeId = $this->getDesignConfig()->getStore();
		}

		if (is_numeric($templateId)) {
			$this->load($templateId);
		} else {
			$localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
			$this->loadDefault($templateId, $localeCode);
		}

		if (!$this->getId()) {
			throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid transactional email code: ' . $templateId));
		}

		if (!is_array($sender)) {
			$this->setSenderName(Mage::getStoreConfig('trans_email/ident_' . $sender . '/name', $storeId));
			$this->setSenderEmail(Mage::getStoreConfig('trans_email/ident_' . $sender . '/email', $storeId));
		} else {
			$this->setSenderName($sender['name']);
			$this->setSenderEmail($sender['email']);
		}

		if (!isset($vars['store'])) {
			$vars['store'] = Mage::app()->getStore($storeId);
		}

		// Added code for custom var example.
		$vars['custom_var'] = 'This is a custom var';
		}

		$this->setSentSuccess($this->send($email, $name, $vars));
		return $this;
	}
}

Template definition (note that this was specified in the config.xml file above)
/app/locale/{language_code}/template/email/order/confirmation.html

<!--@subject {{var store.getFrontendName()}}: New Order # {{var order.increment_id}} @-->
<!--@vars
{"store url=\"\"":"Store Url",
"var logo_url":"Email Logo Image Url",
"var logo_alt":"Email Logo Image Alt",
"htmlescape var=$order.getCustomerName()":"Customer Name",
"var store.getFrontendName()":"Store Name",
"store url=\"customer/account/\"":"Customer Account Url",
"var order.increment_id":"Order Id",
"var order.getCreatedAtFormated('long')":"Order Created At (datetime)",
"var order.getBillingAddress().format('html')":"Billing Address",
"var payment_html":"Payment Details",
"var order.getShippingAddress().format('html')":"Shipping Address",
"var order.getShippingDescription()":"Shipping Description",
"layout handle=\"sales_email_order_items\" order=$order":"Order Items Grid",
"var order.getEmailCustomerNote()":"Email Order Note"}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->

<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">

    {{var order.customFunction()}} <!-- as defined in /app/code/local/{Namespace}/{Module}/Model/Email/Order.php -->
    {{var custom_var}} <!-- as defined in /app/code/local/{Namespace}/{Module}/Model/Email/Template.php -->

    <table cellspacing="0" cellpadding="0" border="0" width="100%">
        <tr>
            <td align="center" valign="top" style="padding:20px 0 20px 0">
                <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                    <!-- [ header starts here] -->
                    <tr>
                        <td valign="top"><a href="{{store url=""}}"><img src="{{var logo_url}}" alt="{{var logo_alt}}" style="margin-bottom:10px;" border="0"/></a></td>
                    </tr>
                    <!-- [ middle starts here] -->
                    <tr>
                        <td valign="top">
                            <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;"">Hello, {{htmlescape var=$order.getCustomerName()}}</h1>
                            <p style="font-size:12px; line-height:16px; margin:0;">
                                Thank you for your order from {{var store.getFrontendName()}}.
                                Once your package ships we will send an email with a link to track your order.
                                You can check the status of your order by <a href="{{store url="customer/account/"}}" style="color:#1E7EC8;">logging into your account</a>.
                                If you have any questions about your order please contact us at <a href="mailto:{{config path='trans_email/ident_support/email'}}" style="color:#1E7EC8;">{{config path='trans_email/ident_support/email'}}</a> or call us at <span class="nobr">{{config path='general/store_information/phone'}}</span> Monday - Friday, 8am - 5pm PST.
                            </p>
                            <p style="font-size:12px; line-height:16px; margin:0;">Your order confirmation is below. Thank you again for your business.</p>
                    </tr>
                    <tr>
                        <td>
                            <h2 style="font-size:18px; font-weight:normal; margin:0;">Your Order #{{var order.increment_id}} <small>(placed on {{var order.getCreatedAtFormated('long')}})</small></h2>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table cellspacing="0" cellpadding="0" border="0" width="650">
                                <thead>
                                <tr>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Billing Information:</th>
                                    <th width="10"></th>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Payment Method:</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var order.getBillingAddress().format('html')}}
                                    </td>
                                    <td>&nbsp;</td>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var payment_html}}
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                            <br/>
                            {{depend order.getIsNotVirtual()}}
                            <table cellspacing="0" cellpadding="0" border="0" width="650">
                                <thead>
                                <tr>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Shipping Information:</th>
                                    <th width="10"></th>
                                    <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Shipping Method:</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var order.getShippingAddress().format('html')}}
                                        &nbsp;
                                    </td>
                                    <td>&nbsp;</td>
                                    <td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">
                                        {{var order.getShippingDescription()}}
                                        &nbsp;
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                            <br/>
                            {{/depend}}
                            {{layout handle="sales_email_order_items" order=$order}}
                            <p style="font-size:12px; margin:0 0 10px 0">{{var order.getEmailCustomerNote()}}</p>
                        </td>
                    </tr>
                    <tr>
                        <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you, <strong>{{var store.getFrontendName()}}</strong></p></center></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</div>
</body>

Having created the module it is then time to create the Transactional Email in the administrator of Magento.
Magento Admin Panel -> System -> Transactional Emails -> Add New Template
The Template select should now allow you to select the template that you have created, it will be visible from the name defined in the config.xml file ().

Once this Template has been added, it can then be set as the default order template.

To achieve this then select magento Admin Panel -> System -> Configuration -> Sales (Sales Emails)
Then expand the Order panel. In the New Order Confirmation select, choose the name that was defined in the previous step when the new template was defined.

That will now give you the opportunity to use custom variables and custom functions in your order transactional emails.

Leave a Reply

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