Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created October 23, 2014 20:43
Show Gist options
  • Save strangerstudios/60e4230db5d9ff9b4c3e to your computer and use it in GitHub Desktop.
Save strangerstudios/60e4230db5d9ff9b4c3e to your computer and use it in GitHub Desktop.
Some code for PMPro to handle cases where users will be checking out for the same level but might be switching from recurring to non or vice versa.
/*
If checking out for a recurring membership for the level the user already has,
set the profile start date to their old end date or the date of their next payment.
*/
function my_pmpro_profile_start_date($startdate, $order) {
//does the user already have this level?
if(is_user_logged_in() && pmpro_hasMembershipLevel($order->membership_id)) {
global $current_user;
//get stats for the user's level
$user_level = pmpro_getMembershipLevelForUser($current_user->ID);
if(!empty($user_level->enddate)) {
$startdate = date('Y-m-d T00:00:00', $user_level->enddate);
} elseif(!empty($user_level->cycle_number)) {
//when is their next payment date
$current_timestamp = current_time('timestamp');
$next_payment_timestamp = pmpro_next_payment($current_user->ID);
if($next_payment_timestamp > $current_timestamp)
$startdate = date('Y-m-d T00:00:00', strtotime('+' . $order->BillingFrequency . ' ' . $order->BillingPeriod, $next_payment_timestamp));
}
}
return $startdate;
}
add_filter('pmpro_profile_start_date', 'my_pmpro_profile_start_date', 10, 2);
/*
If checking out for a non-recurring version of the same level while holding
a recurring version of the level. Add the days until the next payment 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(!empty($level) && !empty($level->expiration_number) && pmpro_hasMembershipLevel($level->id)) {
//get the current enddate of their membership
global $current_user;
//only care if the current user has a recurring level
if(!empty($current_user->membership_level->cycle_number)) {
//when is their next payment date
$todays_date = current_time('timestamp');
$expiration_date = pmpro_next_payment($current_user->ID);
$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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment