Last active
November 5, 2023 14:29
-
-
Save strangerstudios/1c1424baae6ae698d19b to your computer and use it in GitHub Desktop.
Have a discount code only usable for old/expired members in PMPro.
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
/* | |
Old Users Discount Code (oudc) | |
*/ | |
//define the discount code here | |
define('OUDC_DISCOUNT_CODE', 'abc123'); //change abc123 here to the discount code that is for old members only | |
//make sure only old members can use a specific discount code | |
function oudc_pmpro_registration_checks($okay) { | |
//if something else went wrong, just return $okay | |
if(!$okay) | |
return $okay; | |
//if using the oudc, check that they are logged in and an old member | |
if(!empty($_REQUEST['discount_code']) && strtolower($_REQUEST['discount_code']) == strtolower(OUDC_DISCOUNT_CODE)) { | |
global $wpdb, $current_user; | |
//must be logged in | |
if(empty($current_user->ID)) | |
pmpro_setMessage("The discount code entered is for previous members only. Log in to use that code.", "pmpro_error"); | |
else { | |
$old_member = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $current_user->ID . "' AND status NOT IN('cancelled')"); | |
if(empty($old_member)) | |
pmpro_setMessage("The discount code entered is for previous members only whose accounts have expired.", "pmpro_error"); | |
} | |
} | |
return $okay; | |
} | |
add_filter('pmpro_registration_checks', 'oudc_pmpro_registration_checks'); | |
//give the code to old and existing users if discount code is empty | |
function oudc_give_members_code() { | |
if(!is_admin() && !empty($_REQUEST['level']) && empty($_REQUEST['discount_code']) && is_user_logged_in()) { | |
global $wpdb, $current_user; | |
$old_member = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $current_user->ID . "' AND status NOT IN('cancelled')"); | |
if(!empty($old_member)) | |
$_REQUEST['discount_code'] = OUDC_DISCOUNT_CODE; | |
} | |
} | |
add_action('init', 'oudc_give_members_code'); | |
//shortcode to show link to discount for old members | |
function oudc_shortcode() { | |
if(is_user_logged_in()) { | |
global $wpdb, $current_user; | |
$old_member = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $current_user->ID . "' AND status NOT IN('success', 'cancelled')"); | |
if($old_member) { | |
//make sure the level id below matches as well | |
return ' | |
<div><p>As a previous member of our site, you can use the discount code <?php echo OUDC_DISCOUNT_CODE;?> to save 50% on your renewal. <a href="' . pmpro_url('checkout', '?level=1&discount_code=' . OUDC_DISCOUNT_CODE) . '">Click here to renew with this discount code</a>.</p></div> | |
'; | |
} | |
} | |
//return an empty string for existing members and non-members | |
return ""; | |
} | |
add_shortcode('oudc', 'oudc_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Offer Expired and “Old” Members a Membership Renewal Discount for Coming Back" at Paid Memberships Pro here: https://www.paidmembershipspro.com/offer-expired-old-members-membership-renewal-discount-coming-back/