Last active
April 15, 2019 21:52
-
-
Save LMNTL/89cf45cc30928ce06d2ddd9cf4cb145c to your computer and use it in GitHub Desktop.
Allows renewing yearly memberships with a set expiration date. Requires the Set Expiration Dates Add On for Paid Memberships Pro
This file contains 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
/* | |
Allows renewing yearly memberships with a set expiration date. | |
E.g. if the level expires on 1/1/2020 and the member renews their membership before then, their membership will be extended to 1/1/2021 | |
Requires the Set Expiration Dates Add On for Paid Memberships Pro | |
*/ | |
function my_pmpro_yearly_membership_renewal($level) { | |
$extend_level = 1; //change to the level with a set expiration date | |
if( $level->id == $extend_level && pmpro_hasMembershipLevel( $extend_level ) ) | |
{ | |
global $wpdb; | |
$user_id = get_current_user_id(); | |
$sqlQuery = "SELECT enddate FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . esc_sql( $user_id ) . "' AND membership_id = '" . esc_sql( $level->id ) . "' AND status = 'active' ORDER BY id DESC LIMIT 1"; | |
$old_enddate = $wpdb->get_var( $sqlQuery ); | |
$endyear = intval( substr( $old_enddate, 0, 4 ) ) + 1; | |
$new_endtime = $endyear . substr( $old_enddate, 4 ); | |
$new_enddate = strtotime( $new_endtime ); | |
$today = new DateTime(); | |
$new_expiration_date = date_create(); | |
date_timestamp_set( $new_expiration_date, $new_enddate ); | |
// Compute and apply the difference | |
$difference = $new_expiration_date->diff( $today ); | |
$level->expiration_number = $difference->days + 1; | |
$level->expiration_period = 'Days'; | |
} | |
return $level; | |
} | |
add_filter('pmpro_checkout_level', 'my_pmpro_yearly_membership_renewal', 20, 1); | |
function my_pmpro_yearly_membership_renewal_text( $expiration_text, $level) { | |
$extend_level = 1; //change to the level with a set expiration date | |
if( $level->id == $extend_level && pmpro_hasMembershipLevel( $extend_level ) ) | |
{ | |
$enddate = date( get_option('date_format'), strtotime( "+$level->expiration_number days" ) ); | |
$expiration_text = "Membership expires on $enddate."; | |
} | |
return $expiration_text; | |
} | |
add_filter( 'pmpro_level_expiration_text', 'my_pmpro_yearly_membership_renewal_text', 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment