-
-
Save kimwhite/23bf189736eab55bcc73977a120b48d8 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This recipe will add 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 | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
// code starts here | |
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