<?php
/**
 * Block specific user roles from being able to generate referrals.
 *
 * Since this relies on WordPress user roles, the user placing the order
 * must be logged into your site.
 *
 * @link https://andrew.dev/snippets/blocks-specific-user-roles-from-generating-referrals/
 *
 * @return boolean $create_referral False if the user cannot generate a referral, true otherwise.
 */
function am_affwp_block_user_roles_from_generating_referrals( $create_referral ) {

	// Array of roles to exclude. Enter your roles here.
	$excluded_roles = array( 
		'subscriber', 
		'editor'
	);

	// Get the current user.
	$current_user = wp_get_current_user();

	// Get the user's data.
	$user_info = get_userdata( $current_user->ID );

	// Get the user's roles.
	$roles = $user_info->roles;

	// Loop through the user's roles.
	if ( $roles ) {
		foreach ( $roles as $role ) {
			if ( in_array( $role, $excluded_roles ) ) {
				// If the user belongs to an excluded role, set $create_referral to false.
				$create_referral = false;
				break;
			}
		}
	}

	return $create_referral;

}
add_filter( 'affwp_integration_create_referral', 'am_affwp_block_user_roles_from_generating_referrals' );

Leave a Reply

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