Last active
June 21, 2024 17:48
-
-
Save MaximilianoRicoTabo/8b07679223e6fc7482a018e1caf8c6df to your computer and use it in GitHub Desktop.
Add grace period to users that enddate falls in august or christmas
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 | |
/** | |
* Change enddate to a date that is not a holiday give grace period adding more days depending the holidays days. | |
* link: TBD | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
/** | |
* Change enddate to a date that is not a holiday give grace period adding more days depending the holidays days. | |
* | |
* @param int $user_id The user ID. | |
* @param object $morder The order object. | |
*/ | |
function custom_pmpro_after_change_membership_level( $user_id, $morder ) { | |
global $wpdb; | |
$user = get_userdata( $user_id ); | |
//get current year | |
$year = date('Y'); | |
//first of august | |
$august_start = date('Y-m-d', strtotime( $year . '-08-01' ) ); | |
//31 of august | |
$august_end = date('Y-m-d', strtotime( $year . '-08-31' ) ); | |
//22 of december | |
$december_start = date('Y-m-d', strtotime( $year . '-12-22' ) ); | |
//6 of january | |
$january_end = date('Y-m-d', strtotime( $year + 1 . '-01-06' ) ); | |
$today = date( 'Y-m-d' ); | |
//Retrieve memberships user just checked out. | |
$memberships_users = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->pmpro_memberships_users WHERE user_id = %d AND status = 'active' AND | |
startdate >= %s LIMIT 1", $user_id, $today ) ); | |
if ( ! empty( $memberships_users ) ) { | |
$enddate = $memberships_users->enddate; | |
$should_update = false; | |
//Is in august ? Make the end date in september | |
if ( $enddate >= $august_start && $enddate <= $august_end ) { | |
$enddate = date('Y-m-d', strtotime($year . '-09-01')); | |
$should_update = true; | |
} else if ( $enddate >= $december_start && $enddate <= $january_end ) { | |
$enddate = date('Y-m-d', strtotime($year + 1 . '-01-07')); | |
$should_update = true; | |
} | |
if ( $should_update ) { | |
//Update $memberships_users above with new enddate | |
$wpdb->update( $wpdb->pmpro_memberships_users, array( 'enddate' => $enddate ), | |
array( 'id' => $memberships_users->id) ); | |
} | |
} | |
} | |
add_filter( 'pmpro_after_checkout', 'custom_pmpro_after_change_membership_level', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment