Search

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

    Sumit Pore
Listen to this article

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.

 

Creating Referral Links for Registered Members

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.

[space]

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.

[space]

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’.

[space]

Creating a Referral Link with Referral Id

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.

[space]

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.

[space]

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=/";
}

[space]

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.

[space]

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.

Picture of Sumit Pore

Sumit Pore

11 Responses

    1. I tried setting this up as a plugin…but when I go to activate, I get the following error.

      Parse error: syntax error, unexpected T_FUNCTION in /home/content/21/3843821/html/wp-content/plugins/referplugin/referplugin.php on line 62

      This is what is at that line…

      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=/”;
      }

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

      1. Hi Kahil,

        We don’t have a plugin ready. The error you see could be because of the double quotes. Kindly try changing the quotes from “ to

        "
        

        Out of the above code, which is line 62

        1. I actually did change all those out…and I still get that. I’ll play around with the other symbols maybe. I can’t think of anything else that is incorrect.

          Feel free to email me a correct and working code snippet if you would like. 🙂 But I went through and changed out all of the ” and ‘ that were there as they all were invalid.

          1. Did you ever get this to work fully? I changed out all of the ” and ‘ and it still didn’t work. I get the same error. I ran the code through a checker and it said it was fine. (running php 5.9.3)

  1. I have the code from the previous article about creating the system and the code from this page in the same plugin. Do they need to be separate plugins? The instructions weren’t clear on where exactly everything went so I just created a single plugin.

    1. You could add all of the code in a single plugin. I’ll take a look at the code again and let you know if any other changes have to be made.

    1. Hi Calvin,

      The functionality is currently not available as a plugin. Do let me know if you would be interested in a custom solution and we can go forward from there.

    1. Chad, thanks for your interest in the solution and for reading the article. We don’t have a plugin ready, but you can contact us via the Contact Form if you’re interested in a custom solution.

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads