Created
November 29, 2024 15:00
-
-
Save dwanjuki/10875fde319e8a63069f38b12f749b50 to your computer and use it in GitHub Desktop.
Require a discount code to register for a level
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 | |
/** | |
* Require a discount code to register for a level | |
* | |
* 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_checkout_checks_require_code_to_register( $continue ){ | |
// only continue if things are okay so far | |
if( ! $continue ) { | |
return $continue; | |
} | |
global $discount_code; | |
// Get the checkout level id. | |
$checkout_level = pmpro_getLevelAtCheckout(); | |
$checkout_level_id = $checkout_level->id; | |
// ids of levels that require a discount code to check out => the required codes | |
$code_levels = array( | |
1 => array( 'SOMECODE', 'ANOTHERCODE' ), | |
// 4 => array( 'CODE1234', 'CODE5678901' ), | |
); | |
if( array_key_exists( $checkout_level_id, $code_levels ) && ( empty( $discount_code ) || ! in_array( strtoupper( $discount_code ), $code_levels[$checkout_level_id] ) ) ) { | |
pmpro_setMessage( "You must use a valid discount code to register for this level.", "pmpro_error" ); | |
return false; | |
} | |
return $continue; | |
} | |
add_filter( 'pmpro_checkout_checks', 'my_pmpro_checkout_checks_require_code_to_register' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment