Last active
May 11, 2021 19:14
-
-
Save ideadude/ef0256b253482553876ebea70ddecb36 to your computer and use it in GitHub Desktop.
When users expire (and the system changes them to membership level 0) we give them another downgraded level.
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 | |
/** | |
* When users expire (and the system changes them to membership level 0) we give them another downgraded level. | |
* This gist is setup to specifically downgrade level 6 to level 3 with an expiration set to 305 days out and | |
* downgrade level 5 to level 2 with an expiration set to 150 days out. | |
* Note that this code is using the pmpro_membership_post_membership_expiry hook which only fires when a user is expired | |
* by the PMPro cron job. If an admin changes the user's level or they cancel, they will not receive the downgraded level. | |
* Adjust the gist to meet your needs. | |
* Add this code to a custom plugin. https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_membership_post_membership_expiry( $user_id, $level_id ) { | |
// Set this to the id of the level you want to give members when they cancel. | |
$downgrade_level_2 = array( | |
'user_id' => $user_id, | |
'membership_id' => 2, | |
'code_id' => '', | |
'initial_payment' => '', | |
'billing_amount' => '', | |
'cycle_number' => '', | |
'cycle_period' => '', | |
'billing_limit' => '', | |
'trial_amount' => '', | |
'trial_limit' => '', | |
'startdate' => current_time('mysql'), | |
'enddate' => date("Y-m-d", strtotime( '+150 Days', current_time('timestamp') ) ), | |
); | |
$downgrade_level_3 = array( | |
'user_id' => $user_id, | |
'membership_id' => 3, | |
'code_id' => '', | |
'initial_payment' => '', | |
'billing_amount' => '', | |
'cycle_number' => '', | |
'cycle_period' => '', | |
'billing_limit' => '', | |
'trial_amount' => '', | |
'trial_limit' => '', | |
'startdate' => current_time('mysql'), | |
'enddate' => date("Y-m-d", strtotime( '+305 Days', current_time('timestamp') ) ), | |
); | |
// If we see this global set, then another gist is planning to give the user their level back. | |
global $pmpro_next_payment_timestamp; | |
if ( ! empty( $pmpro_next_payment_timestamp ) ) { | |
return; | |
} | |
// Are they cancelling? | |
if ( $level_id == 0 ) { | |
// Check if they are cancelling level 5 or 6. | |
global $wpdb; | |
$last_level_id = $wpdb->get_var( "SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user_id . "' ORDER BY id DESC" ); | |
if( $last_level_id == 5 ) { | |
pmpro_changeMembershipLevel( $downgrade_level_2, $user_id ); | |
} elseif( $last_level_id == 6 ) { | |
pmpro_changeMembershipLevel( $downgrade_level_3, $user_id ); | |
} | |
} | |
} | |
add_filter( 'pmpro_membership_post_membership_expiry', 'my_pmpro_membership_post_membership_expiry', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment