Last active
August 27, 2021 09:15
-
-
Save ipokkel/c81d882a06dd7685ea08cc406a5bdf40 to your computer and use it in GitHub Desktop.
Block specific membership level for old and current members. Hide level from levels page. Redirect level away from checkout page. Stop checkout for level.
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 | |
/** | |
* This recipe will prevent members and users who has had a membership before | |
* from checking out for a specific level. | |
* | |
* 1. Hides level on the Membership Levels page | |
* 2. Redirect back to levels page if user visits checkout page. | |
* 3. Checks on checkout that user neither has nor had the level. | |
* | |
* This is an advanced customization recipe that may require further development | |
* to suit specific needs and/or setups. | |
* | |
* Implementation and further customization may require developer skills. | |
* If you would like, you can reach out to a Partner Developer (working independently from PMPro) | |
* to get an estimate for the project. | |
* | |
* For more details, please see: https://www.paidmembershipspro.com/developers/ | |
* | |
* OPTIONAL: | |
* Forcing existing users to log in before checking out. | |
* @link https://gist.github.com/ipokkel/5676f64f58c29719756d7c7022215332 | |
* | |
* 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/ | |
*/ | |
/** | |
* If a user is or was a member hide level from the levels page. | |
*/ | |
function my_pmpro_hide_specific_level_from_members( $levels ) { | |
if ( ! is_user_logged_in() ) { | |
return $levels; | |
} | |
global $current_user_id; | |
$user_previous_levels = my_pmpro_get_all_previous_membership_levels_for_user( $current_user_id ); | |
/** | |
* Set your level ID here | |
*/ | |
$blocked_level_id = 1; // Set the level to hide here. | |
// hide level | |
if ( pmpro_hasMembershipLevel() || in_array( $blocked_level_id, $user_previous_levels ) ) { | |
// remove if user were a member before | |
unset( $levels[ $blocked_level_id ] ); | |
} | |
return $levels; | |
} | |
add_filter( 'pmpro_levels_array', 'my_pmpro_hide_specific_level_from_members', 10 ); | |
/** | |
* If a user is or was a member redirect them from the checkout page | |
* to the levels page. | |
*/ | |
function my_pmpro_redirect_checkout_of_specific_level_to_levels_page_for_members() { | |
// Lets only check for users who has logged in. | |
if ( ! is_user_logged_in() ) { | |
return; | |
} | |
global $pmpro_pages; | |
if ( ! is_page( $pmpro_pages['checkout'] ) ) { | |
return; | |
} | |
global $pmpro_level; | |
$level = $pmpro_level; | |
/** | |
* Set your level ID here | |
*/ | |
$blocked_level_id = 1; // Set the level to hide here. | |
// are we checking out for this level? | |
if ( intval( $blocked_level_id ) !== intval( $level->id ) ) { | |
return; | |
} | |
// Get all levels user had before. | |
global $current_user_id; | |
$user_previous_levels = my_pmpro_get_all_previous_membership_levels_for_user( $current_user_id ); | |
// Redirect to levels page | |
if ( pmpro_hasMembershipLevel() || in_array( $blocked_level_id, $user_previous_levels ) ) { | |
$levels_page_url = get_permalink( $pmpro_pages['levels'] ); | |
wp_safe_redirect( $levels_page_url ); | |
exit; | |
} | |
} | |
add_action( 'template_redirect', 'my_pmpro_redirect_checkout_of_specific_level_to_levels_page_for_members' ); | |
/** | |
* If a user is or was a member stop them from checking out for the level. | |
*/ | |
function my_pmpro_registration_checks_to_prevent_free_level_checkout_if_member( $okay ) { | |
// Let's only check if things are OK. | |
if ( ! $okay ) { | |
return $okay; | |
} | |
global $pmpro_level; | |
$level = $pmpro_level; | |
/** | |
* Set your level ID here | |
*/ | |
$blocked_level_id = 1; // Set the level to hide here. | |
// are we checking out for this level? | |
if ( intval( $blocked_level_id ) !== intval( $level->id ) ) { | |
return $okay; | |
} | |
// Get all levels user had before. | |
global $current_user_id; | |
$user_previous_levels = my_pmpro_get_all_previous_membership_levels_for_user( $current_user_id ); | |
// Stop checkout for the level. | |
if ( pmpro_hasMembershipLevel() || in_array( $blocked_level_id, $user_previous_levels ) ) { | |
pmpro_setMessage( 'You cannot checkout for this level, please select a different level.', 'pmpro_error' ); | |
$okay = false; | |
} | |
return $okay; | |
} | |
add_action( 'pmpro_registration_checks', 'my_pmpro_registration_checks_to_prevent_free_level_checkout_if_member' ); | |
/** | |
* Get all membership levels the user belonged to. | |
* | |
* @param mixed $user_id, accepts integer or string. | |
* @return array $member_previous_levels | |
*/ | |
function my_pmpro_get_all_previous_membership_levels_for_user( $user_id = null ) { | |
$member_previous_levels = 0; | |
if ( empty( $user_id ) ) { | |
$user_id = get_current_user_id(); | |
} | |
$user_id = intval( $user_id ); | |
if ( is_user_logged_in() ) { | |
global $wpdb; | |
$sql_query = "SELECT DISTINCT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = '$user_id'"; | |
// Check if user previously belonged to a membership level | |
$member_previous_levels = $wpdb->get_results( $sql_query, ARRAY_N ); // get levels | |
$member_previous_levels = array_merge( ...$member_previous_levels ); // flatten multidimensional array | |
} | |
return $member_previous_levels; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment