Created
May 21, 2021 11:16
-
-
Save JarrydLong/b90ee2588f03eda43d373b2fc3fd901d to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* This recipe will add a Restrict field to each discount code. You can then restrict the use of a discount code | |
* when a user is logged in. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function mypmpro_discount_code_restrict_field( $edit ){ | |
?> | |
<table class="form-table"> | |
<tbody> | |
<tr> | |
<th scope="row" valign="top"> | |
<label for="uses">Restrict By Email</label> | |
</th> | |
<td> | |
<textarea name='discount_restrictions' style='width: 30%;' rows='4'><?php echo str_replace( "<br />", "", get_option( 'discount_code_restriction_'.$edit ) ); ?></textarea><br/> | |
<small>One email address per line.</small> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<?php | |
} | |
add_action( 'pmpro_discount_code_after_settings', 'mypmpro_discount_code_restrict_field', 10, 1 ); | |
function mypmpro_save_discount_code_restrict(){ | |
if( isset( $_REQUEST['discount_restrictions'] ) ){ | |
$save_id = intval( $_REQUEST['saveid'] ); | |
$description = nl2br( $_REQUEST['discount_restrictions'] ); | |
update_option( 'discount_code_restriction_'.$save_id, $description ); | |
} | |
} | |
add_action( 'admin_init', 'mypmpro_save_discount_code_restrict' ); | |
function mypmpro_discount_page_header_restrict( $codes ){ | |
echo "<th>Has Restrictions?</th>"; | |
} | |
add_action( 'pmpro_discountcodes_extra_cols_header', 'mypmpro_discount_page_header_restrict', 10, 1 ); | |
function mypmpro_discount_page_column_restrict( $code ){ | |
if( get_option( 'discount_code_restriction_'.$code->id ) !== "" ){ | |
echo "<td>Yes</td>"; | |
} else { | |
echo "<td>No</td>"; | |
} | |
} | |
add_action( 'pmpro_discountcodes_extra_cols_body', 'mypmpro_discount_page_column_restrict', 10, 1 ); | |
function mypmpro_validate_discount_code_use_restrict( $okay, $dbcode, $level_id, $code ){ | |
$discount = new PMPro_Discount_Code( $code ); | |
$restrict = get_option( 'discount_code_restriction_'.$discount->id ); | |
$restrictions = strip_tags( $restrict ); | |
$restrictions = str_replace( "\n", ",", $restrictions ); | |
if( $restrictions !== "" ){ | |
$emails = explode( ",", $restrictions ); | |
global $current_user; | |
if( $current_user ){ | |
if( in_array( $current_user->data->user_email, $emails ) ){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
return $okay; | |
} | |
add_filter( 'pmpro_check_discount_code', 'mypmpro_validate_discount_code_use_restrict', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment