Last active
June 27, 2019 05:52
-
-
Save andrewlimaza/85b03a1c59fa65aca1f2130315d84a82 to your computer and use it in GitHub Desktop.
Add days left to new membership level purchase for Paid Memberships Pro WooCommerce.
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 | |
/** | |
* Calculate days remaining for current membership level and add it to new subscription. | |
* Only works for PMPro WooCommerce Integration | |
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
* www.paidmembershipspro.com | |
*/ | |
function pmprowoo_add_days_to_level( $level_array ) { | |
$level_obj = pmpro_getLevel($level_array['membership_id']); | |
//does this level expire? are they an existing user of this level? | |
if(!empty($level_obj) && !empty($level_obj->expiration_number) ) | |
{ | |
//get the current enddate of their membership | |
$user = get_userdata($level_array['user_id']); | |
$user->membership_level = pmpro_getMembershipLevelForUser($user->ID); | |
$expiration_date = $user->membership_level->enddate; | |
//calculate days left | |
$todays_date = current_time('timestamp'); | |
$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_obj->expiration_period == "Day") | |
$total_days = $days_left + $level_obj->expiration_number; | |
elseif($level_obj->expiration_period == "Week") | |
$total_days = $days_left + $level_obj->expiration_number * 7; | |
elseif($level_obj->expiration_period == "Month") | |
$total_days = $days_left + $level_obj->expiration_number * 30; | |
elseif($level_obj->expiration_period == "Year") | |
$total_days = $days_left + $level_obj->expiration_number * 365; | |
//update the end date | |
$level_array['enddate'] = date("Y-m-d", strtotime("+ $total_days Days", $todays_date)); | |
} | |
} | |
return $level_array; | |
} | |
add_filter('pmprowoo_checkout_level', 'pmprowoo_add_days_to_level'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#Hi Andrew, great code, thank you. Is there a way to exclude a free level that expires in X months from this? otherway this months are added when a user buys a premium level, so they get many "free" months in their subscription. Thank you.