Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/81e04c2f503cf9635c13cfb88cd3f5e3 to your computer and use it in GitHub Desktop.
Save dwanjuki/81e04c2f503cf9635c13cfb88cd3f5e3 to your computer and use it in GitHub Desktop.
Discount Code for Members of Certain Levels Only
<?php // do not copy this line.
/**
* Restrict discount code usage to members of certain levels
*
* 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 my_pmpro_check_discount_code($okay, $dbcode, $level_id, $code) {
// membership levels allowed to use discount codes below
$code_levels = array( 1, 2 );
// member only codes, in UPPERCASE
$member_codes = array( 'YOURCODEHERE' );
if ( in_array( strtoupper( $code ), $member_codes ) && ! pmpro_hasMembershipLevel( $code_levels ) ) {
return 'You must be a member of level 1 or level 2 to use that discount code.'; // your error message
}
return $okay;
}
add_filter( 'pmpro_check_discount_code', 'my_pmpro_check_discount_code', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment