Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbrocks/2840e6721fcdba728b71e3f332d7728a to your computer and use it in GitHub Desktop.
Save pbrocks/2840e6721fcdba728b71e3f332d7728a to your computer and use it in GitHub Desktop.
Change PMPro membership level upon expiration or cancellation to different respective levels based on member's previous level.
<?php // do not copy to Customizations plugin
/**
* After expiration, assign them a specific "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_upon_expiration_change_membership_levels( $level_id, $user_id ) {
// set this to the id of the original level
$last_level_2 = 2;
$last_level_3 = 3;
// set this to the id of the level you want to give members when they cancel
$level_2_cancel_id = 5;
$level_3_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;
}
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_2 === $last_level_id ) {
// otherwise give them level $cancel_level_id instead
pmpro_changeMembershipLevel( $level_2_cancel_id, $user_id );
} elseif ( $last_level_3 === $last_level_id ) {
// otherwise give them level $cancel_level_id instead
pmpro_changeMembershipLevel( $level_3_cancel_id, $user_id );
} else {
return; // let them cancel
}
}
add_action( 'pmpro_membership_post_membership_expiry', 'pmpro_upon_expiration_change_membership_levels', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment