ID === (int) get_current_user_id() ) { $result = null; } } } } } } } return $result; } /** * Invalidate affiliate's own coupons. * * @param boolean $valid * @param WC_Coupon $coupon * @return boolean */ public static function woocommerce_coupon_is_valid( $valid, $coupon ) { global $woocommerce; $code = method_exists( $coupon, 'get_code' ) ? $coupon->get_code() : $coupon->code; $id = method_exists( $coupon, 'get_id' ) ? $coupon->get_id() : $coupon->id; // Only perform checks if the coupon is valid at this stage. if ( $valid && !empty( $coupon ) && !empty( $id ) && !empty( $code ) ) { if ( method_exists( 'Affiliates_Attributes_WordPress', 'get_affiliate_for_coupon' ) ) { self::remove_filters(); if ( $affiliate_id = Affiliates_Attributes_WordPress::get_affiliate_for_coupon( $code ) ) { if ( $user_id = get_current_user_id() ) { if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) { if ( in_array( $affiliate_id, $affiliate_ids ) ) { $valid = false; } } } } self::add_filters(); } } return $valid; } /** * Performs coupon checks after checkout validation. * * @param array $posted posted form data */ public static function woocommerce_after_checkout_validation( $posted ) { global $woocommerce; if ( isset( $woocommerce->cart ) ) { $cart = $woocommerce->cart; if ( ! empty( $cart->applied_coupons ) ) { if ( method_exists( 'Affiliates_Attributes_WordPress', 'get_affiliate_for_coupon' ) ) { $valid = true; $emails = array( $posted['billing_email'] ); if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); $emails[] = $current_user->user_email; } $emails = array_map( 'sanitize_email', array_map( 'strtolower', $emails ) ); self::remove_filters(); foreach ( $cart->applied_coupons as $key => $code ) { $coupon = new WC_Coupon( $code ); if ( ! is_wp_error( $coupon->is_valid() ) ) { $coupon_code = method_exists( $coupon, 'get_code' ) ? $coupon->get_code() : $coupon->code; if ( $affiliate_id = Affiliates_Attributes_WordPress::get_affiliate_for_coupon( $coupon_code ) ) { if ( $user_id = get_current_user_id() ) { if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) { if ( in_array( $affiliate_id, $affiliate_ids ) ) { $valid = false; break; } } } if ( $affiliate = affiliates_get_affiliate( $affiliate_id ) ) { if ( isset( $affiliate['email'] ) && in_array( strtolower( $affiliate['email'] ), $emails ) ) { $valid = false; break; } } } } } self::add_filters(); if ( !$valid ) { $coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED ); unset( $cart->applied_coupons[ $key ] ); $woocommerce->session->coupon_codes = $cart->applied_coupons; $woocommerce->session->refresh_totals = true; } } } } } /** * These filters must be removed so we can get the affiliate id without * their methods interfering when using get_affiliate_for_coupon(). */ private static function remove_filters() { self::$ap_priority = has_filter( 'affiliates_coupon_affiliate_id', array( 'Affiliates_Permanent', 'affiliates_coupon_affiliate_id' ) ); if ( self::$ap_priority !== false ) { remove_filter( 'affiliates_coupon_affiliate_id', array( 'Affiliates_Permanent', 'affiliates_coupon_affiliate_id' ), self::$ap_priority, 2 ); } remove_filter( 'affiliates_coupon_affiliate_id', array( __CLASS__, 'coupon' ), 999 ); } /** * Add the filters back. */ private static function add_filters() { if ( self::$ap_priority !== false ) { add_filter( 'affiliates_coupon_affiliate_id', array( 'Affiliates_Permanent', 'affiliates_coupon_affiliate_id' ), self::$ap_priority, 2 ); } self::$ap_priority = false; add_filter( 'affiliates_coupon_affiliate_id', array( __CLASS__, 'coupon' ), 999, 2 ); } } Affiliates_Exclusion::init();