Skip to content

Instantly share code, notes, and snippets.

@eighty20results
Last active March 14, 2016 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eighty20results/6f7af0968b27844549b8 to your computer and use it in GitHub Desktop.
Save eighty20results/6f7af0968b27844549b8 to your computer and use it in GitHub Desktop.
During checkout, werify whether user has used the specified "trial" membership level before. If so, prevent them from checking out. User must remember to define the level ID for the trial membership in the PMPRO_CUSTOM_TRIAL_LEVEL constant.
<?php
/*
Plugin Name: PMPro Trial Membership Limits
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Prevent users from signing up for the Trial Membership Level more than once
Version: .1
Author: Thomas Sjolshagen w/Stranger Studios <thomas@eighty20results.com>
Author URI: http://www.strangerstudios.com
*/
/*
Only allow users to use the trial level once.
Unless they use a new discount code to checkout.
This is a (significant) variation on this code:
https://gist.github.com/strangerstudios/7cf2d5fa819bb6062b2b
Adding this code as a custom plugin:
http://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
Be sure to change the PMPRO_CUSTOM_TRIAL_LEVEL constant
(the number 2 should be changed to whatever your trial level ID is)
*/
define('PMPRO_CUSTOM_TRIAL_LEVEL', 2); // <-- TODO: Change this to match the level defined as a trial
//check at checkout if the user has used the trial level already
function e20r_pmpro_reg_checks($value)
{
global $current_user;
//set this to the id of your trial level
$trial_level_ids = apply_filters('pmpro_defines_trial_ids', array(PMPRO_CUSTOM_TRIAL_LEVEL));
$current_level = isset($_REQUEST['level']) ? intval($_REQUEST['level']) : 0;
//check if the current user has already used the trial level
if(!empty($current_user->ID) && in_array($current_level, $trial_level_ids))
{
// check status on having used one of the trial membership levels
$previous_trial = get_user_meta($current_user->ID, 'pmpro_trial_level_used', true);
if (empty($previous_trial))
{
$previous_trial = array();
}
elseif (!empty($previous_trial) && !is_array($previous_trial))
{
$previous_trial = array($previous_trial);
}
$already = false;
// has the user signed up w/the trial membership before?
if (!empty($previous_trial) && in_array($current_level, $previous_trial))
{
// yes
$already = true;
}
else
{
// no
$already = false;
if ( in_array($current_level, $trial_level_ids ) )
{
// save the fact that they're joining with a trial level
$previous_trial[] = $current_level;
update_user_meta( $current_user->ID, 'pmpro_trial_level_used', $previous_trial );
}
}
//if they've used a trial level before, don't let them check out now
// and let them know why they're not allowed to check out.
if(true === $already)
{
global $pmpro_msg, $pmpro_msgt;
$pmpro_msg = "You have already used the trial membership. Please select a different membership level.";
$pmpro_msgt = "pmpro_error";
$value = false;
}
}
return $value;
}
add_filter("pmpro_registration_checks", "e20r_pmpro_reg_checks");
//swap the expiration text if the user has used the trial
function e20r_pmpro_level_exp_text($text, $level)
{
global $current_user;
if (empty($current_user->ID))
return $text;
$trial_level_ids = apply_filters('pmpro_defines_trial_ids', array(PMPRO_CUSTOM_TRIAL_LEVEL));
$previous_trial = get_user_meta($current_user->ID, 'pmpro_trial_level_used', true);
if (empty($previous_trial))
{
$previous_trial = array();
}
elseif (!empty($previous_trial) && !is_array($previous_trial))
{
$previous_trial = array($previous_trial);
}
if(!empty($current_user->ID) && in_array($level->id, $trial_level_ids) && in_array($level->id, $previous_trial))
{
$text = "<strong>You have already used your trial membership. Please select a different membership level.</strong>";
}
return $text;
}
add_filter("pmpro_level_expiration_text", "e20r_pmpro_level_exp_text", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment