Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/80a1b5af5161aedd9e85ef95b43ca9d6 to your computer and use it in GitHub Desktop.
Save andrewlimaza/80a1b5af5161aedd9e85ef95b43ca9d6 to your computer and use it in GitHub Desktop.
Place a PMPro member in another level when they cancel... unless they are cancelling from that level.
/*
When users cancel (are changed to membership level 0) we give them another "cancelled" level.
Can be used to downgrade someone to a free level when they cancel.
Will allow members to the "cancel level" to cancel from that though.
*/
function my_pmpro_after_change_membership_level_default_level($level_id, $user_id)
{
//Level ID of membership that you will allow level cancellation on. (Assume this is a free level or lower level than level ID 2 - see line 26)
$cancel_level_id = 1;
//if we see this global set, then another gist is planning to give the user their level back
global $pmpro_next_payment_timestamp;
if(!empty($pmpro_next_payment_timestamp))
return;
//are they cancelling?
if($level_id == 0)
{
//check if they are cancelling from level $cancel_level_id
global $wpdb;
$last_level_id = $wpdb->get_var("SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user_id . "' ORDER BY id DESC");
if($last_level_id == $cancel_level_id)
return; //let them cancel
//otherwise give them level 2 instead.
pmpro_changeMembershipLevel(2, $user_id);
}
}
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level_default_level", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment