Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.