Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidmutero/5425491465578a4415996d76e3e2562b to your computer and use it in GitHub Desktop.
Save davidmutero/5425491465578a4415996d76e3e2562b to your computer and use it in GitHub Desktop.
Custom proration billing amount plus initial amount for a level with a fixed renewal date (like “Y1-10-01”). Requires Subscription Delays Add On.
<?php
/**
* PMPro Customization: Prorate Initial Payment Based on Fixed Renewal Date. requires Subscription Delays Add On
*
* This recipe adjusts the initial payment for a membership level to reflect a prorated charge
* from the join date until a fixed annual renewal date (e.g., October 1).
* The membership level should have its Subscription Delay set to: `Y1-10-01`
*/
function my_pmprosd_prorate_delay( $level ) {
// Only apply to specific level ID(s) — update as needed.
if ( $level->id != 1 ) {
return $level;
}
// Define fixed annual renewal date (MM-DD format).
$subscription_day = "10-01";
$now = new DateTime();
// Skip proration if today is already the renewal date.
if ( $now->format( 'm-d' ) === $subscription_day ) {
return $level;
}
// Determine correct target year for the renewal.
$start_year = $now->format( 'm-d' ) >= $subscription_day ? $now->format( 'Y' ) + 1 : $now->format( 'Y' );
$subscription_start = new DateTime( $start_year . '-' . $subscription_day );
// Calculate how much of the year remains until the renewal date.
$interval_seconds = $subscription_start->getTimestamp() - time();
$proration_fraction = $interval_seconds / ( 365 * 24 * 60 * 60 );
// Avoid overcharging or invalid values.
if ( $proration_fraction <= 0 || $proration_fraction > 1 ) {
return $level;
}
// Dynamically pull billing and signup fee from level settings.
$billing_amount = isset( $level->billing_amount ) ? (float) $level->billing_amount : 0;
$signup_fee = isset( $level->initial_payment ) ? (float) $level->initial_payment : 0;
// Determine base fee portion (signup fee minus one billing cycle, if applicable).
$base_fee = max( 0, $signup_fee - $billing_amount );
// Final calculated prorated initial charge.
$level->initial_payment = round( ( $billing_amount * $proration_fraction ) + $base_fee, 2 );
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