Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active August 31, 2020 17:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save strangerstudios/3678054 to your computer and use it in GitHub Desktop.
Save strangerstudios/3678054 to your computer and use it in GitHub Desktop.
Extend Expiration Dates For Renewed Memberships in Paid Memberships Pro
/*
Important note! This code is included in PMPro versions 1.5.7 and later.
Adding this code will add 2x the number of days to the end of the subscription.
*/
//if checking out for the same level, add remaining days to the enddate
function my_pmpro_checkout_level($level)
{
global $pmpro_msg, $pmpro_msgt;
//does this level expire? are they an existing user of this level?
if($level->expiration_number && pmpro_hasMembershipLevel($level->id))
{
//get the current enddate of their membership
global $current_user;
$expiration_date = $current_user->membership_level->enddate;
//calculate days left
$todays_date = time();
$time_left = $expiration_date - $todays_date;
//time left?
if($time_left > 0)
{
//convert to days and add to the expiration date (assumes expiration was 1 year)
$days_left = floor($time_left/(60*60*24));
//figure out days based on period
if($level->expiration_period == "Day")
$total_days = $days_left + $level->expiration_number;
elseif($level->expiration_period == "Week")
$total_days = $days_left + $level->expiration_number * 7;
elseif($level->expiration_period == "Month")
$total_days = $days_left + $level->expiration_number * 30;
elseif($level->expiration_period == "Year")
$total_days = $days_left + $level->expiration_number * 365;
//update number and period
$level->expiration_number = $total_days;
$level->expiration_period = "Day";
}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");
@strangerstudios
Copy link
Author

This code is built into PMPro versions 1.5.7 and greater. So there is no need to add this code to your setup. If you do, it will actually add 2x the # of days to the end date.

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