Forked from andrewlimaza/next_payment_date_account.php
Last active
November 30, 2021 19:20
-
-
Save kimwhite/6d58c975f318217d69c0a768d2682f55 to your computer and use it in GitHub Desktop.
Show next payment date under the 'Expiration' field on any page (removed if for account page)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Show next payment date under 'Expiration' field on My Membership box | |
* 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 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment