This modules gives visibility on repeat customers in the Magento sales order grid. It does so by adding a repeat customer flag column displaying how many times that customer has purchased.
Most CRMs provide this functionality and allow you to send a thank you after X amount of orders but if you are a small business or don’t want to go that far you may just want to be alerted to the how many orders a customer has made.
Here are the features
- Set the milestones you want to be highlighted i.e. 5th and 10th order
- Be alerted by email when these milestones are hit
- Exclude certain order statuses i.e. cancelled orders.
There is plenty of future development that can be made here i.e. the module can be easily tweaked to automatically send the customer an email thanking them – or generate a coupon code of them to use. But i’ll leave that up to you.
This is what you end up with :
Module System Configuration
File Structure
- app/code/community/Fc/RepeatCustomerFlag
- Block/Adminhtml/Sales/Order/
- Renderer/IsRepeat.php
- Grid.php
- etc/
- adminhtml.xml
- config.xml
- system.xml
- Helper/
- Data.php
- Model/
- System/Config/Source/Adminhtml/Orderstatus.php
- Observer.php
- Block/Adminhtml/Sales/Order/
- app/etc/modules/Fc_RepeatCustomerFlag.xml
- app/locale/en_US/template/email/milestone_hit.html
Module Explanation
Now let’s get down to it. There are only two files we really need to concern ourselves with.
1) app/code/community/Fc/RepeatCustomerFlag/Block/Adminhtml/Sales/Order/Renderer/IsRepeat.php
This is where our new column is rendered to the order grid and where the majority of the heavy lifting is done. This is called on every row of the order grid so we need to ensure that the order isn’t in the excluded order statuses defined in our configuration – otherwise we are adding unnecessary overhead to the order grid.
The column shows two things. The number of orders in total and out of that total which one this order is in the scheme of those orders. i.e. ‘3rd of 6’.
Firstly we need to get a collection of the orders made by our current customer and then we can retrieve their total orders. Secondly, we need to filter the collection by orders made before the date of our current order. The reason for this is so that we can tell out of all the orders if this is the 1st, 2nd etc.
We can then use this to check against the list of configured milestones so we can highlight it appropriately. The rest is pretty simple – we figure out what the suffix for our order number should be (‘st’,’nd’,’rd’ or ‘th’). This is calculated by taking the last digit of the number :
- Ending in ‘1’ = ‘st’
- Ending in ‘2’ = ‘nd’
- Ending in ‘3’ = ‘rd’
- Ending in anything else = ‘th’
And after this we simply compile the html that needs to be displayed in the column.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php /* * Author : Frank Clark * Website : frankclark.xyz */ class Fc_RepeatCustomerFlag_Block_Adminhtml_Sales_Order_Renderer_IsRepeat extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row) { /** @var Fc_RepeatCustomerFlag_Helper_Data $_helper */ $_helper = Mage::helper('fc_repeatcustomerflag'); $ignoredStatuses = $_helper->getIgnoredOrderStatuses(); if(!in_array($row->getStatus(), $ignoredStatuses)) { $orderCollection = $_helper->getCustomerOrders($row); $toDate = $row->getCreatedAt(); $totalOrders = $orderCollection->count(); $orderCollection->addFieldToFilter('created_at', array('lt' => $toDate)); $thisOrder = $orderCollection->getSize() + 1; $milestones = $_helper->getMilestones(); $highlightColour = $_helper->getHighlightColour(); $shouldHighlight = false; if(in_array($thisOrder,$milestones)) { $shouldHighlight = true; } $lastDigit = substr($thisOrder, -1); $numberWithSuffix = $_helper->getNumberSuffix($lastDigit,$thisOrder); $firstOrder = false; if($totalOrders == 0 || $thisOrder == 0) { $firstOrder = true; } $html = '<div style="width:100%; width:100%;'; if($shouldHighlight) { $html .= 'background-color:'.$highlightColour; } $html .= '">'; if($firstOrder) { if($thisOrder == 0) { $html .= '1st order'; } else { $html .= $numberWithSuffix.'</span>'; } } else { $html .= $numberWithSuffix . ' order of '. $totalOrders . '</span>'; } return $html; } return true; } } |
2) app/code/community/Fc/RepeatCustomerFlag/Model/Observer.php
The next important file is the observer that is triggered once an order is created – this is a lot of the same logic as the renderer. We check the order status isn’t excluded and we check that it’s a milestone and if so compile all the information we need to send an administrator an notification email.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php /* * Author : Frank Clark * Website : frankclark.xyz */ class Fc_RepeatCustomerFlag_Model_Observer { public function checkMilestone($event) { /** @var Fc_RepeatCustomerFlag_Helper_Data $_helper */ $_helper = Mage::helper('fc_repeatcustomerflag'); if($_helper->isMilestoneEmailEnabled()) { $order = $event->getOrder(); $orderCollection = $_helper->getCustomerOrders($order); $totalOrders = $orderCollection->count(); $milestones = $_helper->getMilestones(); if(in_array($totalOrders,$milestones)) { try { $this->_sendStatusMail($order,$totalOrders); } catch (Exception $e) { Mage::logException($e); } } } } private function _sendStatusMail($order,$totalOrders) { /** @var Fc_RepeatCustomerFlag_Helper_Data $_helper */ $_helper = Mage::helper('fc_repeatcustomerflag'); $lastDigit = $lastDigit = substr($totalOrders, -1); $milestoneHit = $_helper->getNumberSuffix($lastDigit,$totalOrders); /** @var Mage_Core_Model_Email_Template $emailTemplate */ $emailTemplate = Mage::getModel('core/email_template'); $emailTemplate->loadDefault('milestone_template'); $emailTemplate->setTemplateSubject($order->getCustomerFirstname() . ' ' . $order->getCustomerLastname() .' has hit a order milestone'); $email = $_helper->getMilestoneEmailAddress(); $name = Mage::getStoreConfig('trans_email/ident_general/name'); $emailTemplate->setSenderName($email); $emailTemplate->setSenderEmail($name); $emailTemplateVariables['username'] = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname(); $emailTemplateVariables['order_id'] = $order->getIncrementId(); $emailTemplateVariables['store_name'] = $order->getStoreName(); $emailTemplateVariables['store_url'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); $emailTemplateVariables['milestone'] = $milestoneHit; $emailTemplate->send($email, $order->getStoreName(), $emailTemplateVariables); } } |