Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/30da242dc06baa86c0070db89d6a0f8f to your computer and use it in GitHub Desktop.
Save dwanjuki/30da242dc06baa86c0070db89d6a0f8f to your computer and use it in GitHub Desktop.
Prevent users from checking out for a level they already have, unless the level is expiring soon and the user can renew.
<?php
/**
* Prevent users from checking out for a level they already have,
* unless the level is expiring soon and the user can renew.
*
* 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/
*/
function my_pmpro_same_level_registration_checks( $okay ) {
// bail if things are not okay
if ( ! $okay || ! is_user_logged_in() ) {
return $okay;
}
// get level being purchased
$pmpro_level = pmpro_getLevelAtCheckout();
$level_id = $pmpro_level->id;
// check if user has a membership level and if they do that it isn't expiring soon.
if ( pmpro_hasMembershipLevel( $level_id ) && ! pmpro_isLevelExpiringSoon( $level_id ) ) {
pmpro_setMessage( 'You already have this membership level.', 'pmpro_error' );
$okay = false;
}
return $okay;
}
add_action( 'pmpro_registration_checks', 'my_pmpro_same_level_registration_checks' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment