Forked from kimwhite/pmpro-discount-code-for-membersonly.php
Last active
October 28, 2024 13:41
-
-
Save dwanjuki/81e04c2f503cf9635c13cfb88cd3f5e3 to your computer and use it in GitHub Desktop.
Discount Code for Members of Certain Levels Only
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 // 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