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 LMNTL/b9c5abb8656f5675f269361ecc5e7e4b to your computer and use it in GitHub Desktop.
Save LMNTL/b9c5abb8656f5675f269361ecc5e7e4b to your computer and use it in GitHub Desktop.
Place a PMPro member in another level with a 30 day expiration date when they cancel/expire unless they are cancelling from that level.
function my_pmpro_after_change_membership_level_default_level($level_id, $user_id)
{
//set this to the id of the level you want to give members when they cancel
$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 $cancel_level_id instead
$level = pmpro_getLevel($cancel_level_id);
$level = json_decode(json_encode($level), true);
$level['membership_id'] = $cancel_level_id;
$level['user_id'] = $user_id;
$level['startdate'] = date('Y-m-d');
$level['code_id'] = '';
//give them an expiry date 30 days from today. change the following three lines to change how many days/months/years away the expiry date is
$level['enddate'] = date('Y-m-d',strtotime(date("Y-m-d", time()) . " +30day"));
$level['expiration_number'] = 30;
$level['expiration_period'] = 'days';
pmpro_changeMembershipLevel($level, $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