Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Created March 4, 2020 18:53
Show Gist options
  • Save ronalfy/851ab9b9238ccb250eda096f9409253e to your computer and use it in GitHub Desktop.
Save ronalfy/851ab9b9238ccb250eda096f9409253e to your computer and use it in GitHub Desktop.
Paid Memberships Pro: Prorated Amount Based on Current Time and Beginning of Next Year
<?php
function pmpro_prorated_checkout_level( $level ) {
// 5 is the membership level ID.
if ( 5 === absint( $level->id ) ) {
$price_per_day = $level->initial_payment / 365.2425; // 365.2425 is the days in a year
// Get timestamp for end of year and current timestamp.
$end_date_timestamp = strtotime( 'first day of january next year' );
$current_date_timestamp = time();
// Get the time difference.
$time_difference = $end_date_timestamp - $current_date_timestamp;
// Ensure non negative values.
if ( $time_difference <= 0 ) {
return $level;
}
// Convert timestamp to days.
$days = round( $time_difference / 86400 ); // 86400 is seconds in a day.
// Calculate price left in the year
$prorated_price = round( $days * $price_per_day, 2 );
$level->initial_payment = $prorated_price;
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'pmpro_prorated_checkout_level' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment