Skip to content

Instantly share code, notes, and snippets.

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 strangerstudios/01d6f9b6cf2c40b7838c5a7a37f172b5 to your computer and use it in GitHub Desktop.
Save strangerstudios/01d6f9b6cf2c40b7838c5a7a37f172b5 to your computer and use it in GitHub Desktop.
Create and Manage Affiliates based on membership level when using AffiliateWP and Paid Memberships Pro
<?php
/*
Creates an affiliate for new members of Level ID 1.
Updates affiliate status if the affiliate already exists.
Sets affilaite status to 'inactive' if membership of Level ID 1 is cancelled (includes expiration).
Add this code to a custom plugin.
*/
function my_pmpro_affiliatewp_after_change_membership_level( $level_id, $user_id, $cancel_level ) {
//make sure affiliatewp is active
if(!function_exists('affwp_is_affiliate'))
return;
if($level_id == 1) {
//New member of level 1. Set up the Affiliate
if( affwp_is_affiliate( $user_id ) ) {
$affiliate_id = affwp_get_affiliate_id( $user_id );
//Affiliate already exists. Update Affiliate status to 'active'.
affwp_update_affiliate( array( 'affiliate_id' => $affiliate_id, 'status' => 'active' ) );
} else {
//Affiliate does not exist. Create the Affiliate.
affwp_add_affiliate( array( 'user_id' => $user_id, 'status' => 'active' ) );
}
}
if($cancel_level == 1) {
//User is cancelling or membership is expired.
if( affwp_is_affiliate( $user_id ) ) {
$affiliate_id = affwp_get_affiliate_id( $user_id );
//Affiliate exists. Update Affiliate status to 'inactive'.
affwp_update_affiliate( array( 'affiliate_id' => $affiliate_id, 'status' => 'inactive' ) );
}
}
}
add_action('pmpro_after_change_membership_level', 'my_pmpro_affiliatewp_after_change_membership_level', 10, 3);
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Create and Manage Affiliates by Membership Level Using AffiliateWP" at Paid Memberships Pro here: https://www.paidmembershipspro.com/create-manage-affiliates-membership-level-using-affiliatewp/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment