Home / eCommerce / AffiliateWP — Restrict Checkout Referrals Dropdown by User Role
Duplicate Snippet

Embed Snippet on Your Site

AffiliateWP — Restrict Checkout Referrals Dropdown by User Role

Restricts the AffiliateWP Checkout Referrals dropdown to only appear for logged-in users with a specific WordPress role, and filters the affiliate list to only show affiliates who have a designated role. By default, the dropdown is visible only to users with the 'influencer_sales' role, and only affiliates with the 'influencer' role appear in the list. Update the role slugs in the code to match your setup. Requires AffiliateWP, the Checkout Referrals addon, and WooCommerce.

Code Preview
php
<?php
/**
 * Restrict Checkout Referrals to a Specific User Role & Filter Affiliates by Role
 *
 * - Only displays the Checkout Referrals dropdown to logged-in users
 *   with the 'influencer_sales' role.
 * - Filters the affiliate dropdown to only show affiliates whose WordPress
 *   user account has the 'influencer' role.
 *
 * Works with both WooCommerce classic checkout and Checkout Blocks.
 *
 * Requires: AffiliateWP + Checkout Referrals addon + WooCommerce
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * Part 1a: Hide the classic checkout dropdown for non-influencer_sales users.
 *
 * Runs on 'wp' — after the current user is fully loaded — and removes the
 * Checkout Referrals WooCommerce hook that renders the select/input field.
 */
add_action( 'wp', function() {
	if ( affwp_cr_role_current_user_has_role( 'influencer_sales' ) ) {
		return;
	}
	affwp_cr_role_remove_class_hook(
		'woocommerce_after_order_notes',
		'AffiliateWP_Checkout_Referrals_WooCommerce',
		'affiliate_select_or_input'
	);
	affwp_cr_role_remove_class_hook(
		'woocommerce_checkout_process',
		'AffiliateWP_Checkout_Referrals_WooCommerce',
		'check_affiliate_field'
	);
} );
/**
 * Part 1b: Prevent block checkout field registration for non-influencer_sales users.
 *
 * Fires at priority 1 on woocommerce_init — before the Checkout Referrals addon
 * registers its field at default priority 10.
 */
add_action( 'woocommerce_init', function() {
	if ( affwp_cr_role_current_user_has_role( 'influencer_sales' ) ) {
		return;
	}
	affwp_cr_role_remove_class_hook(
		'woocommerce_init',
		'AffiliateWP_Checkout_Referrals_WooCommerce',
		'register_affiliate_select_or_input_for_checkout_block'
	);
}, 1 );
/**
 * Part 2: Filter the affiliate dropdown to only show users with the 'influencer' role.
 *
 * Uses AffiliateWP's native user_id array support in get_affiliates() to limit
 * results to affiliates whose WordPress account has the 'influencer' role.
 */
add_filter( 'affwp_checkout_referrals_get_affiliates_args', function( $args, $context ) {
	$influencer_user_ids = get_users( array(
		'role'   => 'influencer',
		'fields' => 'ID',
	) );
	if ( empty( $influencer_user_ids ) ) {
		$args['user_id'] = array( 0 );
		return $args;
	}
	$args['user_id'] = array_map( 'absint', $influencer_user_ids );
	return $args;
}, 10, 2 );
/**
 * Helper: Check if the current logged-in user has a specific role.
 */
function affwp_cr_role_current_user_has_role( $role ) {
	if ( ! is_user_logged_in() ) {
		return false;
	}
	$user = wp_get_current_user();
	return in_array( $role, (array) $user->roles, true );
}
/**
 * Helper: Remove a class method from a WordPress hook when the class
 * instance isn't stored in a global variable.
 *
 * Iterates through registered callbacks to find and remove the match.
 */
function affwp_cr_role_remove_class_hook( $hook, $class_name, $method ) {
	global $wp_filter;
	if ( ! isset( $wp_filter[ $hook ] ) ) {
		return false;
	}
	foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) {
		foreach ( $callbacks as $callback_id => $callback ) {
			if ( ! is_array( $callback['function'] ) ) {
				continue;
			}
			if ( ! is_object( $callback['function'][0] ) ) {
				continue;
			}
			if ( get_class( $callback['function'][0] ) !== $class_name ) {
				continue;
			}
			if ( $callback['function'][1] !== $method ) {
				continue;
			}
			remove_action( $hook, $callback['function'], $priority );
			return true;
		}
	}
	return false;
}

Comments

Add a Comment