Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/b949fbbc745bf89e8297033347d19dbb to your computer and use it in GitHub Desktop.
Save andrewlimaza/b949fbbc745bf89e8297033347d19dbb to your computer and use it in GitHub Desktop.
Prorate Paid Memberships Pro Checkouts based on initial payments only.
<?php
/**
* Prorate initial payments (only) for Paid Memberships Pro checkouts.
* This will prorate for any active members changing levels. If the level is the same price, it does not prorate to $0.
* If a member is 'downgrading' levels, set the price to $0. (i.e. $10 -> $5 level will set the initial payment to $0).
*
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function pmpro_prorate_levels_price( $level ) {
global $current_user;
// If user is logged-out, just bail.
if ( ! is_user_logged_in() ) {
return $level;
}
// If user doesn't have an active level, don't prorate.
if ( ! pmpro_hasMembershipLevel() ) {
return $level;
}
$user_level = pmpro_getMembershipLevelForUser( $current_user->ID );
$user_paid = (float) $user_level->initial_payment;
$level_price = (float) $level->initial_payment;
// If they are downgrading, set the price to $0 for now.
if ( $user_paid > $level_price ) {
$level->initial_payment = 0;
return $level;
}
// If the price is exact same as the amount paid, just leave it. We can assume it's a renewal. Remove this code if you want to set it to $0.
if ( $level_price === $user_paid ) {
return $level;
}
// Adjust price (prorated now) to the difference.
$level->initial_payment = $level_price - $user_paid;
return $level;
}
add_filter( 'pmpro_checkout_level', 'pmpro_prorate_levels_price', 20, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment