Skip to content

Instantly share code, notes, and snippets.

@DumahX
Last active April 5, 2021 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DumahX/07e86c2f94789104554265bfe1809dab to your computer and use it in GitHub Desktop.
Save DumahX/07e86c2f94789104554265bfe1809dab to your computer and use it in GitHub Desktop.
<?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