Forked from strangerstudios/my_pmpro_after_change_membership_level.php
Last active
March 21, 2019 12:51
-
-
Save ideadude/a18193e989fb205146782bc3b07a0215 to your computer and use it in GitHub Desktop.
Only allow users to use the trial level once with Paid Memberships Pro.
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 | |
/* | |
Only allow users to use the trial level once. | |
Add this code to your active theme's functions.php | |
or a custom plugin. | |
Be sure to change the $trial_level_id variable in multiple places. | |
*/ | |
//get trial level ids | |
function my_pmpro_trial_level_ids() { | |
// edit this to be a comma separated list of your trial level ids | |
return array(1, 8, 9); | |
} | |
//record when users gain the trial level | |
function my_pmpro_after_change_membership_level($level_id, $user_id) { | |
$trial_level_ids = my_pmpro_trial_level_ids(); | |
if( in_array( $level_id, $trial_level_ids ) ) { | |
//add user meta to record the fact that this user has had this level before | |
update_user_meta($user_id, "pmpro_trial_level_" . $level_id . "_used", "1"); | |
} | |
} | |
add_action("pmpro_after_change_membership_level", "my_pmpro_after_change_membership_level", 10, 2); | |
//check at checkout if the user has used the trial level already | |
function my_pmpro_registration_checks($value) { | |
global $current_user; | |
$trial_level_ids = my_pmpro_trial_level_ids(); | |
if( $current_user->ID && in_array( intval($_REQUEST['level']), $trial_level_ids ) ) { | |
//check if the current user has already used the trial level | |
$already = get_user_meta($current_user->ID, "pmpro_trial_level_" . intval( $_REQUEST['level'] ) . "_used", true); | |
//yup, don't let them checkout | |
if($already) { | |
global $pmpro_msg, $pmpro_msgt; | |
$pmpro_msg = "You have already used up this trial membership. Please select a full membership to checkout."; | |
$pmpro_msgt = "pmpro_error"; | |
$value = false; | |
} | |
} | |
return $value; | |
} | |
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks"); | |
//swap the expiration text if the user has used the trial | |
function my_pmpro_level_expiration_text($text, $level) { | |
global $current_user, $wpdb; | |
$trial_level_ids = my_pmpro_trial_level_ids(); | |
if($current_user->ID && in_array( $level->id, $trial_level_ids ) ) { | |
//check if the current user has already used the trial level | |
$already = get_user_meta($current_user->ID, "pmpro_trial_level_" . $level->id . "_used", true); | |
//yup, don't let them checkout | |
if($already) { | |
$text = "You have already used up this trial membership. Please select a full membership to checkout."; | |
} | |
} | |
return $text; | |
} | |
add_filter("pmpro_level_expiration_text", "my_pmpro_level_expiration_text", 10, 2); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment