Last active
April 16, 2018 19:29
-
-
Save eighty20results/d62e9db199391cb42dd72e61d80135ef to your computer and use it in GitHub Desktop.
Cancel the membership at the end of the month, not immediately.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Change cancellation to set expiration date et expiration date to be the end of the current month, instead of cancelling immediately. | |
Assumes orders are generated for each payment (i.e. your webhooks/etc are setup correctly). | |
*/ | |
//give users their level back with an expiration | |
function e20r_pmpro_after_change_ml($level_id, $user_id) | |
{ | |
//are we on the cancel page? | |
global $pmpro_pages, $wpdb, $pmpro_next_payment_timestamp; | |
if($level_id == 0 && (is_page($pmpro_pages['cancel']) || (is_admin() && (empty($_REQUEST['from']) || $_REQUEST['from'] != 'profile')))) | |
{ | |
/* | |
okay, let's give the user his old level back with an expiration based on his subscription date | |
*/ | |
//get last order | |
$order = new MemberOrder(); | |
$order->getLastMemberOrder($user_id, "cancelled"); | |
//can't do this if we can't find the order | |
if(empty($order->id)) | |
return false; | |
//get the last level they had | |
$level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = '" . $order->membership_id . "' AND user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1"); | |
//can't do this if the level isn't recurring | |
if(empty($level->cycle_number)) | |
return false; | |
//can't do if we can't find an old level | |
if(empty($level)) | |
return false; | |
//last payment date | |
$lastdate = date("Y-m-d", $order->timestamp); | |
/* | |
next payment date | |
*/ | |
$nextdate = strtotime( date('Y-m-t', current_time('timestamp') ), current_time('timestamp') ); | |
//if the date in the future? | |
if($nextdate - time() > 0) | |
{ | |
//give them their level back with the expiration date set | |
$old_level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = '" . $order->membership_id . "' AND user_id = '" . $user_id . "' ORDER BY id DESC LIMIT 1", ARRAY_A); | |
$old_level['enddate'] = date("Y-m-d H:i:s", $nextdate); | |
//disable this hook so we don't loop | |
remove_action("pmpro_after_change_membership_level", "e20r_pmpro_after_change_ml", 10, 2); | |
remove_filter('pmpro_cancel_previous_subscriptions', 'my_pmpro_cancel_previous_subscriptions'); | |
//change level | |
pmpro_changeMembershipLevel($old_level, $user_id); | |
//add the action back just in case | |
add_action("pmpro_after_change_membership_level", "e20r_pmpro_after_change_ml", 10, 2); | |
add_filter('pmpro_cancel_previous_subscriptions', 'my_pmpro_cancel_previous_subscriptions'); | |
//change message shown on cancel page | |
add_filter("gettext", "e20r_cancel_text", 10, 3); | |
} | |
} | |
} | |
add_action("pmpro_after_change_membership_level", "e20r_pmpro_after_change_ml", 10, 2); | |
//this replaces the cancellation text so people know they'll still have access for a certain amount of time | |
function e20r_cancel_text($translated_text, $text, $domain) | |
{ | |
if($domain == "pmpro" && $text == "Your membership has been cancelled.") | |
{ | |
global $current_user; | |
$translated_text = "Your recurring subscription has been cancelled. Your active membership will expire on " . date(get_option("date_format"), pmpro_next_payment($current_user->ID, "cancelled")) . "."; | |
} | |
return $translated_text; | |
} | |
//want to update the cancellation email as well | |
function e20r_email_body($body, $email) | |
{ | |
if($email->template == "cancel") | |
{ | |
global $wpdb; | |
$user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . esc_sql($email->email) . "' LIMIT 1"); | |
if(!empty($user_id)) | |
{ | |
$expiration_date = pmpro_next_payment($user_id); | |
//if the date in the future? | |
if($expiration_date - time() > 0) | |
{ | |
$body .= "<p>Your access will expire on " . date(get_option("date_format"), $expiration_date) . ".</p>"; | |
} | |
} | |
} | |
return $body; | |
} | |
add_filter("pmpro_email_body", "e20r_email_body", 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment