| |
| <?php
|
| add_filter( 'affwp_get_affiliate_rate', function( $rate, $affiliate_id, $type ) {
|
|
|
|
|
| $affiliate_rate = affiliate_wp()->affiliates->get_column( 'rate', $affiliate_id );
|
| if ( ! empty( $affiliate_rate ) ) {
|
| return $rate;
|
| }
|
|
|
|
|
| try {
|
| $group_id = affwp_get_affiliate_group_id( intval( $affiliate_id ) );
|
| } catch ( Exception $e ) {
|
| return $rate;
|
| }
|
|
|
| if ( ! $group_id ) {
|
| return $rate;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| $group_tiers = array(
|
|
|
| 10 => array(
|
| array( 'type' => 'referrals', 'threshold' => 10, 'rate' => 10 ),
|
| array( 'type' => 'referrals', 'threshold' => 25, 'rate' => 15 ),
|
| array( 'type' => 'referrals', 'threshold' => 50, 'rate' => 20 ),
|
| ),
|
|
|
| 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