Skip to content

Instantly share code, notes, and snippets.

@eighty20results
Created August 27, 2016 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eighty20results/438dbf92e2339aeae781b42f1b20e534 to your computer and use it in GitHub Desktop.
Save eighty20results/438dbf92e2339aeae781b42f1b20e534 to your computer and use it in GitHub Desktop.
Prorate the inital payment & set the start date for the recurring payments to the 1st of the month
<?php
/*
Plugin Name: Prorated 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: .1
Author: Thomas Sjolshagen @ Stranger Studios <thomas@eighty20results.com>
Author URI: http://www.eighty20results.com/thomas-sjolshagen
*/
/**
* Set the membership level to account for prorated initial payment (from today until the 1st of next 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')));
$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