Magento Print Customer's Group Name on the transactional emails

Posted by Damodar Bashyal on January 13, 2014

 

One of our Magento clients uses different customer groups to offer different price discounts on orders. When customers buy $100 worth of products within a year they become bronze member with 5% discount on future orders and Silver member if $200 or over and Gold if $300 and over and they get discount accordingly.

Also when they are auto upgraded to next level they are sent welcome letter with member benefits. As pickers and packers won't know if customer has been upgraded. So client wanted an email mentioning that customer has been upgraded, so they can include booklet or something with the order.

As we have our custom module to upgrade customers depending on the requirement. I need to do was just add extra functionality to send email to picker and packers.

On the Observer file (app/code/local/Technooze/Sales/Model/Observer.php) of our upgrade module, I added this condition.

if customer has been upgraded to next level then:

$this->sendGroupUpdateEmailToPickerPacker($customer, $order);

And then created function for that.

public function sendGroupUpdateEmailToPickerPacker($customer, $order){
	$storeId = Mage::app()->getStore()->getId();
	$templateId = 'group upgrade notification';
	$emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateId);
	$vars = array(
		'customer' => $customer,
		'order' => $order
	);
	$emailTemplate->getProcessedTemplate($vars);
	$emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_support/email', $storeId));
	$emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_support/name', $storeId));
	$emailTemplate->send($this->_getEmails(Mage_Sales_Model_Order::XML_PATH_EMAIL_COPY_TO), 'Picker Packer', $vars);
}

protected function _getEmails($configPath)
{
	$data = Mage::getStoreConfig($configPath, Mage::app()->getStore()->getId());
	if (!empty($data)) {
		return explode(',', $data);
	}
	return false;
}

Then copied file: app/code/core/Mage/Sales/Model/Order.php to app/code/local/Mage/Sales/Model/Order.php

And added new function:

// Get customer group name for email template.
public function getCustomerGroupName(){
	if ($groupId = $this->getCustomerGroupId()){
		$group = Mage::getModel ('customer/group')->load($groupId);
		if ($group->getId()){
			return $group->getCode();
		}
	}
	return '';
}

Now you can add {{var order.getCustomerGroupName()}} on transactional emails to get group name e.g. on new order email template app/locale/en_US/template/email/sales/order_new.html

But I needed separated email, so created new transaction email at: Magento admin / System / Transactional Emails

Template Name *: group upgrade notification
Template Subject *: group upgrade notification
Template Content *:

<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;">
<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;"">{{htmlescape var=$order.getCustomerName()}} has been upgraded to <b>{{var order.getCustomerGroupName()}}</b></h1>
            </tr>
            <tr>
                <td>
                    <h2 style="font-size:18px; font-weight:normal; margin:0;">Last 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>
                            {{depend order.getIsNotVirtual()}}
                               <th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Shipping Address:</th>
                            {{/depend}}
                        </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>
                            {{depend order.getIsNotVirtual()}}
                               <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')}}
                               </td>
                            {{/depend}}
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </table>
    </td>
</tr>
</table>
</div>
</body>

And the result email is:

Magento picker Packer Notification email

 
not published on website


QR Code: Magento Print Customer's Group Name on the transactional emails