Created
September 9, 2019 14:26
-
-
Save dparker1005/c77c9ce0085aa0b2d95528eae627537d to your computer and use it in GitHub Desktop.
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
<?php | |
// Copy from below here | |
/** | |
* Automatically applies discount code for users switching from a level in a set of given levels. | |
* Useful for giving discounts to members upgrading their memberships. | |
* To use, make sure to update the first 2 variables in each function. | |
*/ | |
function my_pmpro_automatic_discount_application() { | |
$extended_expiration_discount_code = 'discountcode'; // Change to your discount code. | |
$levels_users_can_switch_from = array( '1', '2' ); // List your level id's that users can switch from. | |
if ( empty( $_REQUEST['level'] ) || ! empty( $_REQUEST['discount_code'] ) || ! pmpro_checkDiscountCode( $extended_expiration_discount_code, $_REQUEST['level'] ) ) { | |
return; | |
} | |
$current_level = pmpro_getMembershipLevelForUser(); | |
if ( isset( $current_level->ID ) && in_array( $current_level->ID, $levels_users_can_switch_from ) ) { | |
$_REQUEST['discount_code'] = $extended_expiration_discount_code; | |
} | |
} | |
add_action( 'init', 'my_pmpro_automatic_discount_application' ); | |
function my_pmpro_check_automatic_disocunt_code( $okay, $dbcode, $level_id, $code ) { | |
$extended_expiration_discount_code = 'discountcode'; // Change to your discount code. | |
$levels_users_can_switch_from = array( '1', '2' ); // List your level id's that users can switch from. | |
if ( $extended_expiration_discount_code === $code ) { | |
$current_level = pmpro_getMembershipLevelForUser(); | |
return in_array( $current_level->ID, $levels_users_can_switch_from ) ? $okay : false; | |
} | |
return $okay; | |
} | |
add_filter( 'pmpro_check_discount_code', 'my_pmpro_check_automatic_disocunt_code', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment