Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active November 30, 2021 19:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save andrewlimaza/b84137ffd15d4bbba530fae5e5b35770 to your computer and use it in GitHub Desktop.
Save andrewlimaza/b84137ffd15d4bbba530fae5e5b35770 to your computer and use it in GitHub Desktop.
Show next payment date under the 'Expiration' field in the PMPro Account Page
<?php
/**
* Show next payment date under 'Expiration' field in PMPro account page.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* Works for PayPal Express and Stripe payment gateways.
* www.paidmembershipspro.com
*/
// Change the expiration text to show the next payment date instead of the expiration date
// This hook is setup in the wp_renewal_dates_setup function below
function my_pmpro_expiration_text($expiration_text) {
global $current_user;
$next_payment = pmpro_next_payment();
if( $next_payment ){
$expiration_text = date_i18n( get_option( 'date_format' ), $next_payment );
}
return $expiration_text;
}
// Change "expiration date" to "renewal date"
// This hook is setup in the wp_renewal_dates_setup function below
function change_expiration_date_to_renewal_date($translated_text, $original_text, $domain) {
if($domain === 'paid-memberships-pro' && $original_text === 'Expiration')
$translated_text = 'Renewal Date';
return $translated_text;
}
// Logic to figure out if the user has a renewal date and to setup the hooks to show that instead
function wp_renewal_dates_setup() {
global $current_user, $pmpro_pages;
// in case PMPro is not active
if(!function_exists('pmpro_getMembershipLevelForUser'))
return;
// If the user has an expiration date, tell PMPro it is expiring "soon" so the renewal link is shown
$membership_level = pmpro_getMembershipLevelForUser($current_user->ID);
if(!empty($membership_level) && !pmpro_isLevelRecurring($membership_level))
add_filter('pmpro_is_level_expiring_soon', '__return_true');
if( is_page( $pmpro_pages[ 'account' ] ) ) {
// If the user has no expiration date, add filter to change "expiration date" to "renewal date"
if(!empty($membership_level) && (empty($membership_level->enddate) || $membership_level->enddate == '0000-00-00 00:00:00'))
add_filter('gettext', 'change_expiration_date_to_renewal_date', 10, 3);
// Check to see if the user's last order was with PayPal Express, else assume it was with Stripe.
// These filters make the next payment calculation more accurate by hitting the gateway
$order = new MemberOrder();
$order->getLastMemberOrder( $current_user->ID );
if( !empty($order) && $order->gateway == 'paypalexpress') {
add_filter('pmpro_next_payment', array('PMProGateway_paypalexpress', 'pmpro_next_payment'), 10, 3);
}else{
add_filter('pmpro_next_payment', array('PMProGateway_stripe', 'pmpro_next_payment'), 10, 3);
}
}
add_filter('pmpro_account_membership_expiration_text', 'my_pmpro_expiration_text');
}
add_action('wp', 'wp_renewal_dates_setup', 11);
@ideadude
Copy link

I sent you some code to update this. It included:

  1. Hooking into "wp" instead of "init" because otherwise is_page() won't work consistently.

  2. Added code to also filter "pmpro_is_level_expiring_soon" to __return_true so renewal links show up. A customer asked for this, and I imagine many others using this code would want it.

  3. I also added a check to the one function in case PMPro isn't active.

  4. I added code to gettext filter "Expiration" to "Renewal Date".

@laurenhagan0306
Copy link

This recipe is included in the blog post on "Display a Membership Renewal Date to your Recurring Members." at Paid Memberships Pro here: https://www.paidmembershipspro.com/display-a-membership-renewal-date-to-your-recurring-members/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment