Last active
January 4, 2022 14:15
-
-
Save strangerstudios/9e2de522c79c9e401e1c to your computer and use it in GitHub Desktop.
Get PMPro WooCommerce to work with PMPro Set Expiration Dates
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
/* | |
Get PMPro WooCommerce to work with PMPro Set Expiration Dates | |
Mostly assumes annually expiring levels. Be careful. | |
Add this code to your active theme's functions.php or a custom plugin. | |
*/ | |
function my_pmprowoo_checkout_level($level_array) | |
{ | |
if(!function_exists('pmpro_getSetExpirationDate')) | |
return $level_array; | |
global $wpdb; | |
//does this level have a set expiration date? | |
$set_expiration_date = pmpro_getSetExpirationDate($level_array['membership_id']); | |
if(!empty($set_expiration_date)) | |
{ | |
//replace vars | |
$now = current_time("timestamp"); | |
$Y = date("Y", $now); | |
$Y2 = intval($Y) + 1; | |
$M = date("m", $now); | |
if($M == 12) | |
$M2 = "01"; | |
else | |
$M2 = str_pad(intval($M) + 1, 2, "0", STR_PAD_LEFT); | |
$searches = array("Y-", "Y2-", "M-", "M2-"); | |
$replacements = array($Y . "-", $Y2 . "-", $M . "-", $M2 . "-"); | |
$set_expiration_date = str_replace($searches, $replacements, $set_expiration_date); | |
//need to extend? | |
if(pmpro_hasMembershipLevel($level_array['membership_id'], $level_array['user_id'])) | |
{ | |
//what's the user's current enddate | |
$current_enddate = $wpdb->get_var("SELECT enddate FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $level_array['user_id'] . "' AND membership_id = '" . $level_array['membership_id'] . "' AND status = 'active' LIMIT 1"); | |
if(!empty($current_enddate)) | |
{ | |
//get info for level | |
$pmpro_level = pmpro_getLevel($level_array['membership_id']); | |
//need timestamp for current end date | |
$current_enddate_ts = strtotime($current_enddate, $now); | |
//extend based on when the enddate is | |
if(date("Y-m-d", $current_enddate_ts) == $set_expiration_date) | |
{ | |
//expiration date is already the set date, just push it out one cycle | |
$set_expiration_date = date("Y-m-d", strtotime($set_expiration_date . " +" . $pmpro_level->expiration_number . " " . $pmpro_level->expiration_period, $now)); | |
} | |
elseif($current_enddate_ts > strtotime($set_expiration_date, $now)) | |
{ | |
//expires after this day? fix date and push it out one more cycle | |
$ex_year = date("Y", $current_enddate_ts); | |
$ex_month = date("m", $current_enddate_ts); | |
$ex_day = date("d", $current_enddate_ts); | |
$set_year = date("Y", strtotime($set_expiration_date, $now)); | |
$set_month = date("m", strtotime($set_expiration_date, $now)); | |
$set_day = date("d", strtotime($set_expiration_date, $now)); | |
//if before set monday/day, keep year + set month/day | |
if($ex_month < $set_month || ($ex_month = $set_month && $ex_day < $set_day)) | |
{ | |
$set_expiration_date = $ex_year . "-" . $set_month . "-" . $set_day; | |
} | |
else | |
{ | |
//if after set month/day, use ex year and set month/day + cycle period | |
$set_expiration_date = date("Y-m-d", strtotime($ex_year . "-" . $set_month . "-" . $set_day . " +" . $pmpro_level->expiration_number . " " . $pmpro_level->expiration_period, $now)); | |
} | |
} | |
} | |
} | |
$level_array['enddate'] = $set_expiration_date; | |
} | |
return $level_array; | |
} | |
add_filter('pmprowoo_checkout_level', 'my_pmprowoo_checkout_level', 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment