Forked from femiyb/pmpro_changeMembershipLevel.php
Last active
March 13, 2023 15:26
-
-
Save kimwhite/65e455c5ebc629af2736298824e9ee6e to your computer and use it in GitHub Desktop.
When users cancel (are changed to membership level 0) we give them another "cancelled" 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 cancel (are changed to membership level 0) we give them another "cancelled" level. | |
* Can be used to downgrade someone to a free level when they cancel. | |
* Will allow members to the "cancel level" to cancel from that though. | |
*/ | |
function pmpro_after_expiration_change_membership_levels( $level_id, $user_id ) { | |
// set this to the id of the level you want to give members when they cancel | |
$last_level_tohave = 5; | |
$level_togive_cancel_id = 6; | |
// 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 ( 0 === $level_id ) { | |
// check if they are cancelling from level $cancel_level_id | |
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 ( (int)$last_level_tohave === (int)$last_level_id ) { | |
// otherwise give them level $cancel_level_id instead | |
pmpro_changeMembershipLevel( $level_togive_cancel_id, $user_id ); | |
} else { | |
return; // let them cancel | |
} | |
} | |
} | |
add_action( 'pmpro_after_change_membership_level', 'pmpro_after_expiration_change_membership_levels', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment