Last active
April 5, 2021 20:11
-
-
Save DumahX/07e86c2f94789104554265bfe1809dab 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 | |
function mepr_restrict_coupons_by_memberships( $errors ) { | |
$coupon = isset( $_POST['mepr_coupon_code'] ) && ! empty( $_POST['mepr_coupon_code'] ) ? $_POST['mepr_coupon_code'] : ''; | |
$user_id = get_current_user_id(); | |
if ( $coupon == 'COUPON' && $user_id == 0 ) { | |
// User doesn't have an account yet, so turn down their coupon attempt. | |
$errors['mepr_coupon_denied'] = 'You cannot use this coupon.'; | |
} | |
$user = new MeprUser( $user_id ); | |
$prods = $user->active_product_subscriptions( 'products' ); | |
$user_prod_ids = array(); | |
$allowed_prod_ids = array( '223' ); // Allowed membership IDs. | |
foreach ( $prods as $prod ) { | |
$user_prod_ids[] = $prod->ID; | |
} | |
if ( $coupon == 'COUPON' && ! array_intersect( $user_prod_ids, $allowed_prod_ids ) ) { | |
// User tried to use coupon, but doesn't belong to the appropriate membership. | |
$errors['mepr_coupon_denied'] = 'You cannot use this coupon.'; | |
} | |
return $errors; | |
} | |
add_filter( 'mepr-validate-signup', 'mepr_restrict_coupons_by_memberships', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment