Forked from strangerstudios/my_pmpro_checkout_level_extend_memberships.php
Last active
April 3, 2021 03:45
-
-
Save kimcoleman/ee1f8551a216574ee1fdc4913716802d to your computer and use it in GitHub Desktop.
Allow membership level expiration date extensions for level changes.
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
<?php | |
/** | |
* Extend the membership expiration date for a member with remaining days on their current level | |
* when they complete checkout for ANY other level that has an expiration date. | |
* Always add remaining days to the enddate. | |
* Pulled in from: https://gist.github.com/3678054 | |
*/ | |
function my_pmpro_checkout_level_extend_memberships( $level ) { | |
global $pmpro_msg, $pmpro_msgt, $current_user; | |
//does this level expire? are they an existing members with an expiration date? | |
if ( ! empty( $level ) && ! empty( $level->expiration_number ) && pmpro_hasMembershipLevel() && ! empty( $current_user->membership_level->enddate ) ) { | |
//get the current enddate of their membership | |
$expiration_date = $current_user->membership_level->enddate; | |
//calculate days left | |
$todays_date = time(); | |
$time_left = $expiration_date - $todays_date; | |
//time left? | |
if ( $time_left > 0 ) { | |
//convert to days and add to the expiration date (assumes expiration was 1 year) | |
$days_left = floor( $time_left/( 60*60*24 ) ) ; | |
//figure out days based on period | |
if ( $level->expiration_period == 'Day' ) { | |
$total_days = $days_left + $level->expiration_number; | |
} elseif ( $level->expiration_period == 'Week' ) { | |
$total_days = $days_left + $level->expiration_number * 7; | |
} elseif ( $level->expiration_period == 'Month' ) { | |
$total_days = $days_left + $level->expiration_number * 30; | |
} elseif ( $level->expiration_period == 'Year' ) { | |
$total_days = $days_left + $level->expiration_number * 365; | |
} | |
//update number and period | |
$level->expiration_number = $total_days; | |
$level->expiration_period = 'Day'; | |
} | |
} | |
return $level; | |
} | |
add_filter( 'pmpro_checkout_level', 'my_pmpro_checkout_level_extend_memberships' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Allow expiring members to extend their membership on renewal or level change." at Paid Memberships Pro here: https://www.paidmembershipspro.com/allow-members-purchase-membership-extension/