Skip to content

Instantly share code, notes, and snippets.

@LMNTL
Created July 16, 2019 20:53
Show Gist options
  • Save LMNTL/d8762f8354f1e08b21f94e794020c0d3 to your computer and use it in GitHub Desktop.
Save LMNTL/d8762f8354f1e08b21f94e794020c0d3 to your computer and use it in GitHub Desktop.
Delete Sponsored Members discount code uses whenever a sponsored member is cancelled/expired
// Delete Sponsored Members discount code uses whenever a sponsored member is cancelled/expired
function my_pmprosm_sponsored_discount_code_reuse($level_id, $user_id) {
global $pmpro_next_payment_timestamp;
// if we see this global is set, other code is planning on giving them their level back
if( !empty($pmpro_next_payment_timestamp) )
return $level_id;
//make sure they're cancelling and they have a sponsored level
if( $level_id == 0 && pmprosm_isSponsoredLevel(pmpro_getMembershipLevelForUser($user_id)->ID) )
{
//get sponsor for user
$sponsor = pmprosm_getSponsor($user_id, false);
if( !empty($sponsor) )
{
//get the discount code for this user
$code_id = pmprosm_getCodeByUserID($sponsor->ID);
if( !empty( $code_id ) )
{
//remove the discount code use and update discount code
pmprosm_removeDiscountCodeUse($user_id, $code_id);
}
}
}
return $level_id;
}
//filter this a little late so $pmpro_next_payment_timestamp is set
add_action( 'pmpro_before_change_membership_level', 'my_pmprosm_sponsored_discount_code_reuse', 12, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment