Cap the number of rewards per advocate

Last updated

Refer a Friend has no built-in limit on how many rewards one advocate can earn. Use this snippet to stop rewarding an advocate after a set number of successful referrals, for example 20.

Before you start: read Add code snippets safely. Checked with version 4.4.12. Works for advocates with a customer account.

Stop rewards after the limit

Change 20 to your limit.

add_filter( 'gens_raf_order_info', function ( $raf_info, $order, $referrer_id ) {
    $max_rewards = 20;

    if ( ! is_numeric( $referrer_id ) ) {
        return $raf_info;
    }

    $referrals = (int) get_user_meta( $referrer_id, 'gens_num_friends', true );
    if ( $referrals >= $max_rewards ) {
        $raf_info['info']               = 'Advocate has reached the maximum number of referral rewards.';
        $raf_info['generate']           = 'false';
        $raf_info['increase_referrals'] = 'false';
    }

    return $raf_info;
}, 10, 3 );

The check runs when the friend places their order. The 21st friend still gets their first-purchase discount, but the advocate gets no coupon. The order’s Refer a Friend Data box says “Advocate has reached the maximum number of referral rewards.”

Optionally, replace the link with a message

Add this as well to show a message instead of the referral link once the advocate reaches the limit. Use the same number.

add_filter( 'wpgens_raf_link', function ( $raf_link, $referral_id, $type ) {
    $max_rewards = 20;

    $referrals = (int) get_user_meta( get_current_user_id(), 'gens_num_friends', true );
    if ( is_user_logged_in() && $referrals >= $max_rewards ) {
        return 'You have reached the maximum number of friends you can refer.';
    }

    return $raf_link;
}, 10, 3 );

The message appears where the link usually is. Share buttons and the referral code may still show, and links the advocate already shared keep working for their friends.

Check it worked

Find an advocate under Advocates, note their referral count, and temporarily set $max_rewards to that number. Place a referral order with their link. The order’s Refer a Friend Data box should show the limit message. Set the number back afterwards.

Related

Browse our plugins

Lightweight WooCommerce plugins built for speed. No bloat, no frameworks -- just clean code that works.

View all plugins
Stay in the loop

Get notified when we launch new plugins. No spam, just product updates.