Last active
November 5, 2025 06:14
-
-
Save JarrydLong/dd04e640692bae53833d79a96e28c220 to your computer and use it in GitHub Desktop.
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 //do not copy | |
| /** | |
| * 1. Block checkout immediately if user already has the same level. | |
| */ | |
| function my_pmpro_prevent_same_level_on_load() { | |
| global $pmpro_pages; | |
| // Only run on checkout page and for logged-in users | |
| if ( ! is_page( $pmpro_pages['checkout'] ) || ! is_user_logged_in() ) { | |
| return; | |
| } | |
| global $current_user, $pmpro_level; | |
| // No level selected, skip | |
| if ( empty( $pmpro_level ) || empty( $pmpro_level->id ) ) { | |
| return; | |
| } | |
| // Check if user already has this level | |
| if ( function_exists( 'pmpro_hasMembershipLevel' ) && pmpro_hasMembershipLevel( $pmpro_level->id, $current_user->ID ) ) { | |
| // Set an error message | |
| pmpro_setMessage( | |
| sprintf( | |
| __( 'You already have the "%s" membership level.', 'paid-memberships-pro' ), | |
| $pmpro_level->name | |
| ), | |
| 'pmpro_error' | |
| ); | |
| // Redirect back to the levels page (or home) after setting the message | |
| // wp_redirect( pmpro_url( 'levels' ) ); | |
| // exit; | |
| } | |
| } | |
| add_action( 'pmpro_checkout_preheader', 'my_pmpro_prevent_same_level_on_load' ); | |
| // 2. Prevent registration/checkout from proceeding if same level | |
| function block_same_level_registration_check($okay) { | |
| global $pmpro_level, $current_user; | |
| if ( ! $okay || ! is_user_logged_in() || empty($pmpro_level) ) { | |
| return $okay; | |
| } | |
| $level_id = $pmpro_level->id; | |
| if ( pmpro_hasMembershipLevel($level_id, $current_user->ID) | |
| && ! pmpro_isLevelExpiringSoon($level_id) ) { | |
| pmpro_setMessage(__('You already have this membership level.', 'paid-memberships-pro'), 'pmpro_error'); | |
| return false; | |
| } | |
| return $okay; | |
| } | |
| add_action('pmpro_registration_checks', 'block_same_level_registration_check'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment