Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created March 5, 2012 21:00
Show Gist options
  • Save strangerstudios/1981053 to your computer and use it in GitHub Desktop.
Save strangerstudios/1981053 to your computer and use it in GitHub Desktop.
Membership Renewals/Extensions in Paid Memberships Pro
<?php
/*
Renewal/Extension Code for PMPro
If the user checking out currently has the same level he is checking out for, the initial payment is zeroed out and the subscription is setup to start on the date of the user's next payment.
Note this only works for monthly and annual subscriptions now. The my_getNextPaymentDate function would have to be updated to correctly calculate for subscriptions with periods > 1 (e.g. every 3 months), or daily or weekly subscriptions.
*/
//if checking out for the same level, waive the initial payment
function my_pmpro_checkout_level($level)
{
global $pmpro_msg, $pmpro_msgt;
//are they an existing user?
if(pmpro_hasMembershipLevel($level->id))
{
//no initial payment
$level->initial_payment = 0;
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");
//if checking out for the same level, set the subscription date to the next payment date
function my_pmpro_profile_start_date($date, $order)
{
//if the user has an existing membership level
global $current_user, $wpdb;
if(pmpro_hasMembershipLevel($order->membership_id))
{
//get the date of their last order
$subscription_start_date = $wpdb->get_var("SELECT startdate FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $current_user->ID . "' LIMIT 1");
if(!empty($subscription_start_date))
{
$date = my_getNextPaymentDate();
}
}
return $date;
}
add_filter("pmpro_profile_start_date", "my_pmpro_profile_start_date", 10, 2);
function my_getNextPaymentDate($format = "Y-m-d")
{
global $wpdb, $current_user;
//get what day are their payments on?
$subscription_start_date = $wpdb->get_var("SELECT startdate FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $current_user->ID . "' LIMIT 1");
$subscription_day = date("d", strtotime($subscription_start_date));
//next month or next year
if($current_user->membership_level->cycle_period == "Month")
{
//next month
$next_month = strtotime(" + 1 Month");
$days_in_next_month = date("t", $next_month);
if($days_in_next_month > $subscription_day)
$date = date("Y-m", $next_month) . "-" . $subscription_day . "T0:0:0";
else
$date = date("Y-m", $next_month) . "-" . $days_in_next_month . "T0:0:0";
}
else
{
//next year
$next_year = strtotime(" + 1 Year");
$days_in_that_month = date("t", $next_year);
if($days_in_that_month > $subscription_day)
$date = date("Y-m", $next_year) . "-" . $subscription_day . "T0:0:0";
else
$date = date("Y-m", $next_year) . "-" . $days_in_that_month . "T0:0:0";
}
if($format == "Y-m-d")
return $date;
elseif($format == "timestamp")
return strtotime($date);
else
return date($format, strtotime($date));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment