Last active
April 23, 2024 12:59
-
-
Save ipokkel/8a44ce3ce7f31e84c1f789878a21a703 to your computer and use it in GitHub Desktop.
Stop and prevent members from checking out for a membership level they are already a member of.
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 specific set of levels if they already have that level. | |
* | |
* Set the $blocked_levels array to the level ids you want to block in each function. | |
* | |
* 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/ | |
*/ | |
// Warn the user on the checkout page if they already have a membership level in the checkout levels. | |
function my_pmpro_checkout_set_message_has_level() { | |
global $current_user, $pmpro_level, $pmpro_checkout_level_ids, $pmpro_msg, $pmpro_msgt; | |
// Add the level ids to stop registration for here. | |
$blocked_levels = array( 1, 2, 3 ); | |
// bail if necessary. | |
if ( ! is_user_logged_in() || ! pmpro_hasMembershipLevel( $blocked_levels ) || ! empty( $pmpro_msg ) ) { | |
return; | |
} | |
$checkout_levels = array(); | |
// get the checkout levels | |
if ( ! empty( $pmpro_checkout_level_ids ) ) { | |
$checkout_levels = $pmpro_checkout_level_ids; | |
} elseif ( ! empty( $pmpro_level ) ) { | |
$checkout_levels = array( $pmpro_level->id ); | |
} | |
// get all membership level ids for user | |
$user_levels = pmpro_getMembershipLevelsForUser( $current_user->ID ); | |
$user_level_ids = array(); | |
foreach ( $user_levels as $user_level ) { | |
$user_level_ids[] = $user_level->id; | |
} | |
// get the checkout level ids matching the user's membership level ids | |
$has_levels = array_intersect( $user_level_ids, $checkout_levels ); | |
// get the has levels matching the blocked levels | |
$has_levels = array_intersect( $has_levels, $blocked_levels ); | |
// bail if user does not have a membership level in the checkout levels | |
if ( empty( $has_levels ) ) { | |
return; | |
} | |
// Let's let the user know they already have the level. | |
if ( empty( $pmpro_msg ) ) { | |
foreach ( $has_levels as $has_level ) { | |
// get the array key for the user_levels where have level matches the object->id | |
$key = array_search( $has_level, array_column( $user_levels, 'id' ) ); | |
$level = $user_levels[ $key ]; | |
// Set the message. | |
/* translators: %s: membership level name */ | |
$alert_message = sprintf( __( 'You are already a member of the <strong>%s</strong> membership level.', 'paid-memberships-pro' ), $level->name ); | |
pmpro_setMessage( $alert_message, 'pmpro_error' ); | |
break; | |
} | |
} | |
} | |
add_action( 'pmpro_checkout_after_parameters_set', 'my_pmpro_checkout_set_message_has_level' ); | |
// Block users from registering for the same level they already have, unless the level is expiring soon and the user can renew. | |
function my_pmpro_same_level_registration_checks( $okay ) { | |
global $pmpro_level; | |
// Add the level ids to stop registration for here. | |
$blocked_levels = array( 1, 2, 3 ); | |
// bail if things are not okay | |
if ( ! $okay || ! is_user_logged_in() ) { | |
return $okay; | |
} | |
$level_id = $pmpro_level->id; | |
// check if level id is in blocked levels and if user already have a blocked membership level set message. | |
if ( in_array( $level_id, $blocked_levels ) && pmpro_hasMembershipLevel( $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' ); | |
// Remove all member action links for specific levels. | |
function my_pmpro_remove_member_action_links_levels( $pmpro_member_action_links, $level_id ) { | |
// Add the level ids to remove action links for here: | |
$blocked_levels = array( 1, 2, 3 ); | |
if ( in_array( $level_id, $blocked_levels ) ) { | |
$pmpro_member_action_links = array(); | |
} | |
return $pmpro_member_action_links; | |
} | |
add_filter( 'pmpro_member_action_links', 'my_pmpro_remove_member_action_links_levels', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment