Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save strangerstudios/7526391 to your computer and use it in GitHub Desktop.
Save strangerstudios/7526391 to your computer and use it in GitHub Desktop.
By default PMPro will only "extend" a membership level if you checkout for the same level. This code will make it so PMPro extends any expiring level with any new expiring level. E.g. if you change from a annual plan to a monthly plan it will add 30 days to the end of your existing membership.
<?php
/*
If checking out for ANY level with an expiration, add remaining days to the enddate.
Pulled in from: https://gist.github.com/3678054
*/
function my_pmpro_checkout_level_extend_memberships($level)
{
global $pmpro_msg, $pmpro_msgt, $current_user;
//does this level expire? are they an existing members with an expiration date?
if(!empty($level) && !empty($level->expiration_number) && pmpro_hasMembershipLevel() && !empty($current_user->membership_level->enddate))
{
//get the current enddate of their membership
$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_extend_memberships");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment