Skip to content

Instantly share code, notes, and snippets.

@codelion7
Last active April 10, 2020 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelion7/ae9771c4f789eb41527bb1ba1e303f2b to your computer and use it in GitHub Desktop.
Save codelion7/ae9771c4f789eb41527bb1ba1e303f2b to your computer and use it in GitHub Desktop.
Require a Specific Rank & Product Purchase to Start a New Cycle
// Make sure AffiliateWP is active
if ( function_exists( 'affiliate_wp' ) ) {
// Make sure AffiliateWP MLM is active
//if ( ! class_exists( 'AffiliateWP_Multi_Level_Marketing' ) ) return;
/**
* Require a Specific Rank & Product Purchase to Start a New Cycle
*
* @since 1.0
* @return bool
*/
function affwp_cust_require_cycle_rank( $allowed, $affiliate_id, $downline, $current_cycle, $cycles ) {
// Make sure the current cycle is complete
if ( $allowed != true ) return $allowed;
// Make sure AffiliateWP Ranks and WooCommerce are active
if ( ! ( function_exists( 'affwp_ranks_has_higher_rank' ) || function_exists( 'wc_customer_bought_product' ) ) ) return $allowed;
if ( empty( $current_cycle ) || empty( $affiliate_id ) ) return $allowed;
$user_id = affwp_get_affiliate_user_id( $affiliate_id );
$user = get_user_by( 'id', $user_id );
$email = $user->user_email;
if ( $current_cycle >= 5 ) return $allowed;
if ( $current_cycle == 1 ) {
$rank_id = 7014;
$product_id = 24940; // Upgrade to Cycle 2
}
if ( $current_cycle == 2 ) {
$rank_id = 3834;
$product_id = 24942; // Upgrade to Cycle 3
}
if ( $current_cycle == 3 ) {
$rank_id = 3003;
$product_id = 24944; // Upgrade to Cycle 4
}
if ( $current_cycle == 4 ) {
$rank_id = 4004;
$product_id = 24946; // Upgrade to Cycle 5
}
// Prevent new cycle if the affiliate has not earned the rank or a higher one
if ( affwp_ranks_has_rank( $affiliate_id, $rank_id ) || affwp_ranks_has_higher_rank( $affiliate_id, $rank_id ) ) {
// Check for product purchase
if ( ! wc_customer_bought_product( $email, $user_id, $product_id ) ) $allowed = false;
} else {
$allowed = false;
}
return $allowed;
}
add_filter( 'affwp_mlm_new_cycle_allowed', 'affwp_cust_require_cycle_rank', 10, 5 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment