Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pbrocks/cdc322273633439ff2cd0d423160370d to your computer and use it in GitHub Desktop.
Save pbrocks/cdc322273633439ff2cd0d423160370d to your computer and use it in GitHub Desktop.
No proration for the initial payment & set the start date for the recurring payments to the 1st of the month
<?php
/**
* Plugin Name: UNprorated initial membership payment
* Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
* Description: Will prorate the membership payment from today until the 1st of the next month.
* Version: .2.1
* Author: Thomas Sjolshagen @ Stranger Studios <thomas@eighty20results.com>
*/
/**
* Keep the membership level payment same as a full month.
*
* @param stdClass $level - The membership level definition to update
*
* @return stdClass (level)
*/
function e20r_prorate_initial_payment( $level ) {
if ( false == pmpro_hasMembershipLevel() ) {
// user does not have level already, but we want to prorate initial payment for rest of the month
// today
$today = current_time( 'timestamp' );
// when would the first subscription payment be
$next_payment_date = strtotime( 'first day of next month', current_time( 'timestamp' ) );
// how many days left
// $days_left = ceil( ( $next_payment_date - $today ) / DAY_IN_SECONDS );
// convert to percent
// $percent = $days_left / ( $next_payment_date - strtotime( 'first day of this month', current_time( 'timestamp' ) ) );
// Let's remove the proration
$percent = $days_left / $days_left;
$level->initial_payment = round( $level->initial_payment * $percent, 2 );
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'e20r_prorate_initial_payment', 11, 1 );
/**
* Set the startdate for the next payment for use by the payment gateway (i.e. 1st of next month)
*
* @param string $startdate The default start date for the subscription plan
* @param MemberOrder $order The order object
*
* @return string The date to use
*/
function e20r_prorate_set_startdate( $startdate, $order ) {
if ( false == pmpro_hasMembershipLevel() ) {
$startdate = date( 'Y-m-01', strtotime( date( 'Y-m-01', current_time( 'timestamp' ) ) . ' + ' . $order->BillingFrequency . ' ' . $order->BillingPeriod ) ) . 'T0:0:0';
}
return $startdate;
}
add_filter( 'pmpro_profile_start_date', 'e20r_prorate_set_startdate', 11, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment