Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JarrydLong/55b7ecdb3dadc2bcaa188e78c8883e52 to your computer and use it in GitHub Desktop.

Select an option

Save JarrydLong/55b7ecdb3dadc2bcaa188e78c8883e52 to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* This recipe ensures that the member can only hold one level across all groups.
* Signing up for more than one group across any level groups will cancel all other levels.
*
* 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/
*
*/
/**
* Cancel memberships from other groups when a user checks out for a new one.
* Keeps memberships in the same group.
*/
function my_pmpro_cancel_other_group_memberships( $user_id, $morder ) {
// Get all memberships the user currently holds (after checkout this may include both).
$user_levels = pmpro_getMembershipLevelsForUser( $user_id );
if ( empty( $user_levels ) ) {
return;
}
$new_level = pmpro_get_group_id_for_level( $morder->membership_id );
foreach ( $user_levels as $level ) {
// Skip the new level (we don’t want to cancel it).
if ( $level->id == $morder->membership_id ) {
continue;
}
$current_level = pmpro_get_group_id_for_level( $level->id );
if( $current_level == $new_level ) {
continue; // Same group, skip cancellation.
}
// Cancel the membership.
pmpro_cancelMembershipLevel( $level->id, $user_id );
}
}
add_action( 'pmpro_after_checkout', 'my_pmpro_cancel_other_group_memberships', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment