Forked from greathmaster/pmpro-sponsored-members-workaround-for-expiring-discount-code.php
Created
June 21, 2019 17:55
-
-
Save itsjusteileen/a2f5a9bf80e834f165eae640ede5a6d6 to your computer and use it in GitHub Desktop.
PMPro Sponsored Members workaround for expiring discount code.
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
/* | |
If a parent account checks out and distributes the discount code to 3/10 of its child accounts, | |
the parent account should still have 7 remaining. This 7 remaining uses should persist after a | |
renewal by the parent account. | |
By default the discount codes have an expiration period of one year. | |
Even if they renew, the discount code expiration date is not updated. | |
We are hard coding the start and end date of the discount code itself in the function pmprosm_createSponsorCode() | |
This code snippet gives 10 year expiration for the discount code. | |
CAUTION: Setting the expiration date beyond ~17 years may cause issues on 32-bit systems due to integer size limits. | |
*/ | |
//cancel sub members when a main account cancels | |
//activate sub members when changed to main account | |
//generate a discount code when changing to main account level | |
function my_pmprosm_pmpro_after_change_membership_level($level_id, $user_id) | |
{ | |
global $wpdb; | |
//are they cancelling? | |
if(empty($level_id)) | |
{ | |
//is there a discount code attached to this user? | |
$code_id = pmprosm_getCodeByUserID($user_id); | |
//if so find all users who signed up with that and cancel them as well | |
if(!empty($code_id)) | |
{ | |
$sqlQuery = "SELECT user_id FROM $wpdb->pmpro_discount_codes_uses WHERE code_id = '" . $code_id . "'"; | |
$sub_user_ids = $wpdb->get_col($sqlQuery); | |
if(!empty($sub_user_ids)) | |
{ | |
foreach($sub_user_ids as $sub_user_id) | |
{ | |
//cancel their membership | |
pmpro_changeMembershipLevel(0, $sub_user_id); | |
} | |
} | |
} | |
//remove seats from meta | |
update_user_meta($user_id, "pmprosm_seats", ""); | |
} | |
elseif(pmprosm_isMainLevel($level_id)) | |
{ | |
//get values for this sponsorship | |
$pmprosm_values = pmprosm_getValuesByMainLevel($level_id); | |
//check if this user already has a discount code | |
$code_id = pmprosm_getCodeByUserID($user_id); | |
//make sure the code is still around | |
if($code_id) | |
{ | |
$code_exists = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE id = '" . $code_id . "' LIMIT 1"); | |
if(!$code_exists) | |
$code_id = false; | |
} | |
//no code, make one | |
if(empty($code_id)) | |
{ | |
//if seats cost money and there are no seats, just return | |
if(!empty($pmprosm_values['seat_cost']) && empty($_REQUEST['seats'])) | |
return; | |
//check for seats | |
if(isset($_REQUEST['seats'])) | |
$uses = intval($_REQUEST['seats']); | |
elseif(!empty($pmprosm_values['seats'])) | |
$uses = $pmprosm_values['seats']; | |
elseif(!empty($pmprosm_values['min_seats'])) | |
$uses = $pmprosm_values['min_seats']; | |
else | |
$uses = ""; | |
//create a new code | |
my_pmprosm_createSponsorCode($user_id, $level_id, $uses); | |
//make sure seats is correct in user meta | |
update_user_meta($user_id, "pmprosm_seats", $uses); | |
} | |
elseif(!empty($pmprosm_values['sponsored_level_id'])) | |
{ | |
//update sponsor code and sub accounts | |
pmprosm_sponsored_account_change($level_id, $user_id); | |
//make sure we only do it once | |
remove_action("pmpro_after_checkout", "pmprosm_pmpro_after_checkout_sponsor_account_change", 10, 2); | |
} | |
} | |
} | |
add_action("pmpro_after_change_membership_level", "my_pmprosm_pmpro_after_change_membership_level", 10, 2); | |
remove_action("pmpro_after_change_membership_level", "pmprosm_pmpro_after_change_membership_level", 10, 2); | |
/* | |
Create a new sponsor discount code. | |
*/ | |
function my_pmprosm_createSponsorCode($user_id, $level_id, $uses = "") { | |
global $wpdb; | |
//get values for this sponsorship | |
$pmprosm_values = pmprosm_getValuesByMainLevel($level_id); | |
//generate a new code. change these values if you want. | |
if(version_compare(PMPRO_VERSION, "1.7.5") > 0) | |
$code = "S" . pmpro_getDiscountCode($user_id); //seed parameter added in version 1.7.6 | |
else | |
$code = "S" . pmpro_getDiscountCode(); | |
$starts = date("Y-m-d", current_time("timestamp")); | |
$expires = date("Y-m-d", strtotime("+10 year", current_time("timestamp"))); | |
$sqlQuery = "INSERT INTO $wpdb->pmpro_discount_codes (code, starts, expires, uses) VALUES('" . esc_sql($code) . "', '" . $starts . "', '" . $expires . "', '" . intval($uses) . "')"; | |
if($wpdb->query($sqlQuery) !== false) | |
{ | |
//set code in user meta | |
$code_id = $wpdb->insert_id; | |
pmprosm_setCodeUserID($code_id, $user_id); | |
//okay update levels for code | |
if(!is_array($pmprosm_values['sponsored_level_id'])) | |
$sponsored_levels = array($pmprosm_values['sponsored_level_id']); | |
else | |
$sponsored_levels = $pmprosm_values['sponsored_level_id']; | |
foreach($sponsored_levels as $sponsored_level) | |
{ | |
//default values for discount code; everything free | |
$discount_code = array( | |
'code_id'=>esc_sql($code_id), | |
'level_id'=>esc_sql($sponsored_level), | |
'initial_payment'=>'0', | |
'billing_amount'=>'0', | |
'cycle_number'=>'0', | |
'cycle_period'=>"'Month'", | |
'billing_limit'=>'0', | |
'trial_amount'=>'0', | |
'trial_limit'=>'0', | |
'expiration_number'=>'0', | |
'expiration_period' => "'Month'" | |
); | |
//allow override of the discount code values by setting it in the pmprosm_sponsored_account_levels array | |
if(!empty($pmprosm_values['discount_code'])) | |
foreach($discount_code as $col => $value) | |
if(isset($pmprosm_values['discount_code'][$col])) | |
$discount_code[$col] = $pmprosm_values['discount_code'][$col]; | |
$sqlQuery = "INSERT INTO $wpdb->pmpro_discount_codes_levels (code_id, | |
level_id, | |
initial_payment, | |
billing_amount, | |
cycle_number, | |
cycle_period, | |
billing_limit, | |
trial_amount, | |
trial_limit, | |
expiration_number, | |
expiration_period) | |
VALUES(" . implode(",", $discount_code) . ")"; | |
$wpdb->query($sqlQuery); | |
} | |
//code created | |
return $code_id; | |
} | |
//something went wrong | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment