Higher Commission Rate for a User Role (AffiliateWP)
Gives affiliates with a specific user role, such as "partner" or "wholesale", a different commission rate from the global AffiliateWP rate, unless they have a custom rate set on their profile.
Opens WPIDE on
Code
$wpide_role = 'partner'; // User role that gets the special rate.
$wpide_rate = 30; // Rate for that role (30 = 30% for percentage rates).
add_filter( 'affwp_get_affiliate_rate', function ( $rate, $affiliate_id ) use ( $wpide_role, $wpide_rate ) {
if ( ! function_exists( 'affwp_get_affiliate' ) ) {
return $rate;
}
$affiliate = affwp_get_affiliate( $affiliate_id );
if ( ! $affiliate || '' !== (string) $affiliate->rate ) {
return $rate; // A custom rate on the affiliate wins.
}
$user = get_userdata( $affiliate->user_id );
if ( $user && in_array( $wpide_role, (array) $user->roles, true ) ) {
$type = function_exists( 'affwp_get_affiliate_rate_type' ) ? affwp_get_affiliate_rate_type( $affiliate_id ) : 'percentage';
return 'percentage' === $type ? $wpide_rate / 100 : $wpide_rate;
}
return $rate;
}, 10, 2 );
Notes
The rate uses the affiliate's rate type (percentage or flat). Affiliates with their own rate in AffiliateWP keep it.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.