Control who gets a referral link

By default every logged-in customer gets a referral link and code. This page shows how to limit that, starting with the built-in settings.

Built-in settings

Goal Setting
Only customers who have bought before General Settings → Hide referral link for users without any orders. Customers without a Processing, Completed or On Hold order see No orders text instead.
Let guests get a link too General Settings → Enable referral links for guests. See Guest referrals.
Block specific people Fraud Settings → Emails to Ban. They see Message for banned users. instead of their link, and their existing links stop giving rewards.
Turn the program off for everyone General Settings → Disable Referral Program.

Hide referral link for users without any orders also stops the Create referrals tool from creating codes for customers without orders.

Limit it by role, subscription or a list of people

For other rules, use this snippet. Read Add code snippets safely first. Checked with version 4.4.12.

Keep one of the example rules in my_raf_user_can_refer() and change the message.

function my_raf_user_can_refer( $user_id ) {
    $user = get_userdata( $user_id );
    if ( ! $user ) {
        return false;
    }

    // Example 1: only customers with the "editor" role, plus admins.
    return in_array( 'editor', (array) $user->roles, true ) || user_can( $user, 'manage_options' );

    // Example 2: only customers with an active subscription (WooCommerce Subscriptions).
    // return function_exists( 'wcs_user_has_subscription' )
    //  && ( wcs_user_has_subscription( $user_id, '', 'active' ) || wcs_user_has_subscription( $user_id, '', 'pending-cancel' ) );

    // Example 3: everyone except these people.
    // return ! in_array( strtolower( $user->user_email ), array( '[email protected]', '[email protected]' ), true );

    // Example 4: admins only, while you test the program.
    // return user_can( $user, 'manage_options' );
}

function my_raf_blocked_message() {
    return 'The referral program is available only to members.';
}

// Replace the code with the message for customers who can't refer.
add_filter( 'wpgens_raf_code', function ( $code ) {
    $owner = get_users( array(
        'meta_key'   => 'gens_referral_id',
        'meta_value' => $code,
        'number'     => 1,
        'fields'     => 'ID',
    ) );
    if ( $owner && ! my_raf_user_can_refer( (int) $owner[0] ) ) {
        return my_raf_blocked_message();
    }
    return $code;
} );

// Replace the link too.
add_filter( 'wpgens_raf_link', function ( $link, $code ) {
    return $code === my_raf_blocked_message() ? my_raf_blocked_message() : $link;
}, 10, 2 );

// Hide the Refer a Friend tab in My Account.
add_action( 'wp', function () {
    if ( ! is_user_logged_in() || my_raf_user_can_refer( get_current_user_id() ) || ! class_exists( 'WPGens_RAF' ) ) {
        return;
    }
    remove_filter( 'woocommerce_account_menu_items', array( WPGens_RAF::instance()->my_account, 'gens_account_menu_item' ), 10 );
} );

The snippet checks the owner of each code, not the person viewing the page. That keeps it correct in places where nobody is logged in, such as the reminder email.

What customers who can’t refer see

  • The message instead of their code and link, in My Account, shortcodes and the product tab.
  • No Refer a Friend tab in My Account.

Links they shared before you added the snippet still work, and still reward them. To stop those as well, add their emails to Emails to Ban.

Check it worked

Log in as a customer who shouldn’t have a link and open My Account. The Refer a Friend tab should be gone, and a product page’s Refer a Friend tab should show your message. Then log in as a customer who should have a link, and check that it’s there.

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.