E-Commerce WordPress Solutions

Refer-a-Friend Reward System for WooCommerce: Tracking Referral Links

A refer a friend program turns happy customers into a sales channel. Here is how to build a referral reward system for WooCommerce with link tracking, so word of mouth becomes measurable, rewardable growth.

Sumit Pore Sumit Pore 4 min read

Reward Friend Referral WooCommerceAs mentioned in our previous article, an introduction to creating a referral system add on to WooCommerce Points and Rewards extension, we learnt about creating a pay-per-referral kind of system. In this article, we will learn about rewarding customers, based on referral conversions. Referral conversions would mean, a referred visitor has performed some action on the shopping site, such as become a member, or purchased an item.

If we consider that each member should have the possibility of referring customers, we need to create a unique referral link for each member. A member can then use the referral link to advertise about the store. Referral links should be generated for existing members, as well as for new members possibly at the time of account creation.

Assigning Points for Referral Conversions

In the previous article, we had created an option in the ‘Points Earned for Actions’ setting, to assign points for Refer-a-Friend. We need to create a similar option, and assign points for referral conversions.

Generate a Unique Id for Member

A unique id, can be generated using the username and some random numbers. This value can be saved as user meta information. Saving this information as user meta, allows us easy access to the value when needed.

do{
    $usr_ref_id = wdm_generate_referral_id($username);
}while (wdm_exists_ref_id($usr_ref_id));
update_user_meta( $user_id, 'wdm_store_referral_id', $usr_ref_id );

function wdm_generate_referral_id($randomString="refid", $length = 4)
{
    $characters = "0123456789";
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

The ‘wdm_exists_ref_id’ function should check, if referral id has been assigned to any other user. This can be done by checking if the meta value generated already exists, for the meta key ‘wdm_store_referral_id’.

Instead of using the default value of ‘$randomString’, it is better to use a substring created with the user name. For example, if user name is ‘John Webber’, $randomString can be sent as ‘jwebber’.

Using the referral id, we have to generate referral links for each member. You can use ‘get_home_url’, to get the url of the site.

// for example: if $usr_ref_id = abc123, and the site is 'example.com', the link created will be http://example.com/?refid=abc123
esc_url(add_query_arg( 'refid', $usr_ref_id, get_home_url() ))

A section can be provided under every user account, where the referral link, and terms and conditions can be placed. This section can be displayed on ‘woocommerce_after_my_account’ hook.

Tracking Referral Conversions

To track if a referred friend has made a purchase on the site, we need to verify, if he was brought to the site using a referral link. For this, we need to check if the referral id was present in the link parameters, and save the value in a cookie.

Saving the Referral Id

When a user visits the shopping site, you need to read the parameter values in the url. If there is a ‘refid’ parameter, you need to save the value, in a cookie.

function setCookie(cvalue) {
    // cookie is set to expire after 30 days
    var expdays = 30;
    var d = new Date();
    d.setTime(d.getTime() + (expdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = "wdmreferralid=" + cvalue + "; " + expires + ";path=/";
}

Rewarding the Referrer when Purchase is Made

When a visitor makes a purchase (or signs up as a member), we need to reward points to the referring customer. This can be done only if the cookie value (for ‘wdmreferralid’) is a member’s referral id.

if (isset($_COOKIE['wdmreferralid'])) {
    $refid = $_COOKIE['wdmreferralid'];
    // get user with the refid
    // award the points using WC_Points_Rewards_Manager
}

Do note, we need to take into account certain conditions, before awarding points. For example, we need to check if the customer making the purchase, is not using his own referral link.

Conclusion

This is a very basic implementation of a referral system. There are many more use cases you can consider here. But nevertheless it would work well if implemented correctly. The main part here is to ensure that a referrer is rightfully awarded, when a referral performs the required action on the site.

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *