Skip to content

Instantly share code, notes, and snippets.

@LMNTL
Created May 6, 2019 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LMNTL/aa181e55110be9790bb90884e1ece4f2 to your computer and use it in GitHub Desktop.
Save LMNTL/aa181e55110be9790bb90884e1ece4f2 to your computer and use it in GitHub Desktop.
Custom proration for a level with a subscription delay until a specified date (like "Y1-08-01"). Requires Paid Memberships Pro and Subscription Delays Add On.
// Custom proration for levels with a subcription delay until a specified date (like "Y1-08-01").
// Prorates initial payment based on days until end of subscription delay for level 1
function my_pmprosd_prorate_delay( $level )
{
// change this to the ID of the membership level to prorate
if( $level->id == 1 ){
// change this to the day of year the subscription delay ends, in MM-DD format
$subscription_day = "08-01";
$now = new DateTime();
// don't prorate if today is when the delay ends
if( $now->format("Y-m-d") == ( $now->format('Y') . "-" . $subscription_day ) ){
return $level;
}
$end_year = intval( $now->format('Y') );
if( $now->format('m-d') >= $subscription_day ){
$end_year += 1;
}
$subscription_start = new DateTime( strval( $end_year ) . "-" . $subscription_day );
$interval_seconds = $subscription_start->getTimeStamp() - time();
// how much should we prorate them?
$proration_amount = $interval_seconds / (24 * 60 * 60 * 365);
// don't prorate if we're charging more than the base cost or it's going to lead to a negative/zero initial payment
if( $proration_amount > 1 || $proration_amount <= 0 )
return $level;
$level->initial_payment = $level->initial_payment*( $proration_amount );
}
return $level;
}
add_filter('pmpro_checkout_level', 'my_pmprosd_prorate_delay');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment