Home / eCommerce / Tiered Affiliate Rates — Per-Group Tier Structures
Duplicate Snippet

Embed Snippet on Your Site

Tiered Affiliate Rates — Per-Group Tier Structures

Applies group-specific tiered commission rates. Each affiliate group can have its own set of tier thresholds and rates, independent of the global Tiered Affiliate Rates settings.

Code Preview
php
<?php
add_filter( 'affwp_get_affiliate_rate', function( $rate, $affiliate_id, $type ) {
    // Respect individual rates -- if the affiliate has one set manually, skip group tiers.
    $affiliate_rate = affiliate_wp()->affiliates->get_column( 'rate', $affiliate_id );
    if ( ! empty( $affiliate_rate ) ) {
        return $rate;
    }
    // Get the affiliate's group ID.
    try {
        $group_id = affwp_get_affiliate_group_id( intval( $affiliate_id ) );
    } catch ( Exception $e ) {
        return $rate;
    }
    if ( ! $group_id ) {
        return $rate;
    }
    // ---------------------------------------------------------------
    // Define group-specific tier structures.
    //
    // Replace the array keys (10, 11) with your actual group IDs.
    // Find the ID by going to AffiliateWP > Groups, clicking a group
    // to edit, and reading the ?id= value in the URL.
    //
    // Each tier:
    //   'type'      => 'referrals' or 'earnings'
    //   'threshold' => minimum count or dollar amount to reach this tier
    //   'rate'      => commission percentage as a whole number (20 = 20%)
    //
    // List tiers from lowest to highest -- the snippet picks the highest met.
    // ---------------------------------------------------------------
    $group_tiers = array(
        // Group A (e.g. "Silver") -- replace 10 with actual group ID
        10 => array(
            array( 'type' => 'referrals', 'threshold' => 10, 'rate' => 10 ),
            array( 'type' => 'referrals', 'threshold' => 25, 'rate' => 15 ),
            array( 'type' => 'referrals', 'threshold' => 50, 'rate' => 20 ),
        ),
        // Group B (e.g. "Gold") -- replace 11 with actual group ID
        11 => array(
            array( 'type' => 'referrals', 'threshold' => 5,  'rate' => 15 ),
            array( 'type' => 'referrals', 'threshold' => 20, 'rate' => 20 ),
            array( 'type' => 'referrals', 'threshold' => 40, 'rate' => 25 ),
        ),
    );
    if ( ! isset( $group_tiers[ $group_id ] ) ) {
        return $rate;
    }
    $tiers_expire = null !== affiliate_wp()->settings->get( 'rate-expiration', null );
    if ( $tiers_expire ) {
        $start     = date( 'Y-m-d H:i:s', strtotime( 'first day of', current_time( 'timestamp' ) ) );
        $end       = date( 'Y-m-d H:i:s', current_time( 'timestamp' ) );
        $earnings  = affiliate_wp()->referrals->paid_earnings( 'month', $affiliate_id, false );
        $referrals = affiliate_wp()->referrals->count( array(
            'affiliate_id' => absint( $affiliate_id ),
            'status'       => 'paid',
            'date'         => array( 'start' => $start, 'end' => $end ),
        ) );
    } else {
        $earnings  = affwp_get_affiliate_earnings( $affiliate_id, false );
        $referrals = affwp_get_affiliate_referral_count( $affiliate_id );
    }
    foreach ( array_reverse( $group_tiers[ $group_id ] ) as $tier ) {
        if ( empty( $tier['threshold'] ) || empty( $tier['rate'] ) ) {
            continue;
        }
        $threshold_met = ( 'earnings' === $tier['type'] )
            ? $earnings  >= affwp_sanitize_amount( $tier['threshold'] )
            : $referrals >= intval( $tier['threshold'] );
        if ( $threshold_met ) {
            return 'percentage' === $type ? $tier['rate'] / 100 : $tier['rate'];
        }
    }
    return $rate;
}, 11, 3 );

Comments

Add a Comment