Forked from ipokkel/pmpro-prevent-checkout-for-current-levels.php
Last active
October 1, 2024 07:41
-
-
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.
This file contains 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 | |
/** | |
* 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