Search

Create a Refer-a-Friend Reward System for WooCommerce

Listen to this article

Reward Friend Referral WooCommerceThe WooCommerce Points and Rewards extension is a really useful plugin, when it comes to adding a points reward system to your WooCommerce store. Such a system, allows you, as the store owner, to reward points to your customers, for performing some action on the site. For example, points can be awarded for every purchase made, or for taking a poll, signing-up for a monthly newsletter, etc. These points can be then redeemed by the customer, for a credit amount, or a discount.

[space]

Create a Custom ‘Refer-a-Friend-for-Rewards’ Plugin

This Points and Rewards extension plugin for WooCommerce, inherently provides settings which allow you to assign points individually for every product purchase, upon a visitor registering for an account on the site, and upon a customer writing a review. But to award points for other actions, the plugin has a ‘Points Earned for Actions’ section, which can be used, to add points for a specific action.

To extend the functionality of the extension, it is best to create your own plugin. This article will guide you through the process of creating a custom buddy referral system, which will award points to every user, for the following actions:

  • Referring a Friend
  • Rewarding Referral Purchases
  • Rewarding Referral Sign-Ups

Since this plugin is an extension to the WooCommerce Points and Rewards plugin, you will not need to worry much about saving and assigning points for a particular action performed. You can just make use of the APIs provided by the extension, to assign points for user actions.

All you would need to do is relate actions to points.

[space]

Points Awarded For Referring a Friend

To begin with, there needs to be a Refer-a-Friend option, such as a form, either at the product level, or at the site, or user account level, using which a customer can refer a friend. Basically, the customer will have to fill up the buddy referral form, and an automated email will be sent, to the referred friend. Here, upon submitting the form, ‘x’ points should be awarded to the customer.

To award points, the store owner has to set a value for buddy referrals. This value can be set in an option under the ‘Points Earned for Actions’ setting.

// add a setting to allow store owner to set points for friend referral
add_filter( 'wc_points_rewards_action_settings', 'wdm_points_rewards_friend_referral_settings' );
function wdm_points_rewards_friend_referral_settings( $settings ) {
    $settings[] = array(
    'title'    => __( 'Points earned for Referring a Friend' ),
    'desc_tip' => __( 'Enter the amount of points earned when a customer refers a friend.' ),
    'id'       => 'wdm_points_rewards_friend_referral',
    );
    return $settings;
}

[space]

The next step is to actually assign the points, upon a customer referring a friend. For this, we have to add an action upon referral form submission, which will award points to the logged in user, using the WC_Points_Rewards_Manager class.

// on submitting the form, we need to add the following function.
add_action('wdm_after_refer_friend_submit', 'wdm_points_rewards_friend_referral_action' );
function wdm_points_rewards_friend_referral_action( $referred_friend ) {
    if ( is_user_logged_in() ){
      // get the points associated with friend referral action
      $points = get_option( 'wdm_points_rewards_friend_referral' );
      if ( ! empty( $points ) ) {
        WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, 'refer-a-friend-success', $referred_friend );
      }
    }
}

Here, ‘increase_points’ function, successfully adds assigned points to the logged in user’s account, for the custom action (buddy referral form submission). The ‘refer-a-friend-success’ should be the slug, which you will use for your custom action. Additional data ($referred_friend), can be sent along with the function, which you would need for your reference.

[space]

One last step. Since your function will add some points as rewards, we need to add a description for this event, for logging purposes.

<?php
add_filter('wc_points_rewards_event_description', 'add_points_rewards_newsletter_action_event_description', 10, 3 );
function add_points_rewards_newsletter_action_event_description( $event_description, $event_type, $event ) {
    $points_label = get_option( 'wc_points_rewards_points_label' );
    switch ( $event_type ) {
      case 'refer-a-friend-success': $event_description = sprintf( __( '%s earned for referring a friend.' ), $points_label ); break;
    }
    return $event_description;
}

[space]

Extending the Buddy Referral System: Using Referral Links

Now that we have covered one use case, the next is to track a referral signing-up, or making purchases. Such a use case is a bit more complicated than rewarding points for buddy referrals. This is similar to creating an affiliate program, which will allow you to track customers converted using referral links. To provide this feature, we will need to first understand some basics of a referral system.

[space]

Why use Referral Links?

Referral links allow converted customers to be associated, with registered users, who referred them. Thus allowing, the possibility to award a registered user, points, every time a referred customer performs some action.

To uniquely identify a referral link, a referral id may be used, when creating the link. For example, if you are a registered customer for ‘xyz’ shopping website. You will have a referral link for your account. It might look something like this ‘http://xyz.com/aff?id=128’. Here, the link is for ‘xyz’ shop, and ‘128’ would be your referral id.

When you refer a friend, an email will be sent to the friend, with the referral link. To earn points from the referral, the friend has to visit the site ‘xyz’, using (by clicking) the referral link, and performing some action (like making a purchase or signing up).

[space]

How are Referral Visits Tracked using Referral Links?

Every user that clicks a referral link and goes to ‘xyz’, will be identified as a referred visitor. To track if a referred friend has made a purchase, we need to make use of data sent in the referral link, and set a cookie, in the visitor’s browser. When a purchase is made, we need to check if the cookie is set, i.e., check if the customer was a referral, and award points accordingly.

But consider this case. Say, a user, clicks on the referral link, visits the site, but doesn’t make a purchase right away. Instead, he visits the site after several days (without using the referral link), and makes a purchase. In this case, even though the visitor was referred by a customer, at the time of making the purchase, the referral link wasn’t used. Yet, the referring customer can be rightfully awarded, if the cookie still exists in the visitor’s browser. We can achieve this by increasing the lifespan of the cookie (as required) when saving it.

[space]

What’s next? Well, next we will learn how to set cookies and use the data to reward the appropriate customer. That you can read in the next half of this article. You can state any questions you have up till now, in the comment section below.

Sumit Pore

Sumit Pore

10 Responses

  1. Hi, Thanks for this wonderful explanation. Just wondering how do you link the “action upon referral form submission” to a actual form submission? Do i have to create a custom form by myself? Thank you.

    1. Hi John,

      Thanks for the comment. To track a form submission, you could create a custom form but you don’t have to. You could use a form builder plugin and hook on to a form submission hook. For example if you create a referral form using Gravity Forms, you could use the ‘gform_pre_submission_5’ hook (where 5 is the form ID), to track form submissions.

      1. Thank you so much for your reply! Really appreciate it.

        I am using contact form 7, but it doesn’t seem to work. Wonder if you could help me out. See below for what i tried to do.

        add_action( ‘wpcf7_mail_sent’, ‘your_wpcf7_mail_sent_function’ );

        function your_wpcf7_mail_sent_function() {
        $title = $contact_form->title;
        $posted_data = $contact_form->posted_data;

        if ( ‘Contact form 2’ == $title ) {
        // on submitting the friend referral form, we need to add the following function.
        function wdm_points_rewards_friend_referral_action( $referred_friend ) {
        if ( is_user_logged_in() ){
        // get the points associated with friend referral action
        $points = get_option( ‘wdm_points_rewards_friend_referral’ );
        if ( ! empty( $points ) ) {
        WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, ‘refer-a-friend-success’, $referred_friend );
        }
        }
        }
        add_action(‘wdm_after_refer_friend_submit’, ‘wdm_points_rewards_friend_referral_action’ );
        }
        }

        1. Hi John,

          Try this code

          add_action( 'wpcf7_mail_sent', 'your_wpcf7_mail_sent_function' ); 
          
          function your_wpcf7_mail_sent_function( $contact_form ) {
              $title = $contact_form->title;
              if ( 'Contact form 2' == $title ) {
                 // on submitting the friend referral form, we need to add the following function.
                if ( is_user_logged_in() ){
                   // get the points associated with friend referral action
                   $points = get_option( 'wdm_points_rewards_friend_referral' );
                   if ( ! empty( $points ) ) {
                        WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, 'refer-a-friend-success', $referred_friend );
                    }
                }
             }
          }
          
      1. Hello Sumit Pore, I looked in my theme and all I could find was Custom Functions.php. When I go to the parent theme, I see functions.php. I entered the code at the bottom of the file and the site had code on the top. What all code do I copy? All three potions mentioned in the article? Sorry. I am new to wordpress and “coding”. FYI, I am using the cherry framework with a cherry theme. Thank you in advance.

  2. Hi,

    Thanks for such a good explanation!

    I did all the steps mentioned above to extend my point-&-rewards to give point upon referral, but it’s not working i am not sure what am i doing wrong please HELP as it would be much appreciate a big thanks in advance. (here is the code i put in my functions file):

    // add a setting to allow store owner to set points for friend referral
    add_filter( ‘wc_points_rewards_action_settings’, ‘wdm_points_rewards_friend_referral_settings’ );
    function wdm_points_rewards_friend_referral_settings( $settings ) {
    $settings[] = array(
    ‘title’ => __( ‘Points earned for Referring a Friend’ ),
    ‘desc_tip’ => __( ‘Enter the amount of points earned when a customer refers a friend.’ ),
    ‘id’ => ‘wdm_points_rewards_friend_referral’,
    );
    return $settings;
    }

    add_action( ‘wpcf7_mail_sent’, ‘your_wpcf7_mail_sent_function’ );

    function your_wpcf7_mail_sent_function( $contact_form ) {
    $title = $contact_form->title;
    if ( ‘Referral Form’ == $title ) {
    // on submitting the friend referral form, we need to add the following function.
    if ( is_user_logged_in() ){
    // get the points associated with friend referral action
    $points = get_option( ‘wdm_points_rewards_friend_referral’ );
    if ( ! empty( $points ) ) {
    WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, ‘refer-a-friend-success’, $referred_friend );
    }
    }
    }
    }

    // on submitting the form, we need to add the following function.
    add_action(‘wdm_after_refer_friend_submit’, ‘wdm_points_rewards_friend_referral_action’ );
    function wdm_points_rewards_friend_referral_action( $referred_friend ) {
    if ( is_user_logged_in() ){
    // get the points associated with friend referral action
    $points = get_option( ‘wdm_points_rewards_friend_referral’ );
    if ( ! empty( $points ) ) {
    WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, ‘refer-a-friend-success’, $referred_friend );
    }
    }
    }

    add_filter(‘wc_points_rewards_event_description’, ‘add_points_rewards_newsletter_action_event_description’, 10, 3 );
    function add_points_rewards_newsletter_action_event_description( $event_description, $event_type, $event ) {
    $points_label = get_option( ‘wc_points_rewards_points_label’ );
    switch ( $event_type ) {
    case ‘refer-a-friend-success’: $event_description = sprintf( __( ‘%s earned for referring a friend.’ ), $points_label ); break;
    }
    return $event_description;
    }

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

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

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