Repeat Customer Flag

Posted on January 17, 2016
Written by

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 :

Repeat Customer Flag

 

Module System Configuration

Repeat Customer Flat 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
  • 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.

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.