Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jasperf/69024401cb38458b84242c1951b4742f to your computer and use it in GitHub Desktop.
Save jasperf/69024401cb38458b84242c1951b4742f to your computer and use it in GitHub Desktop.
User cannot use coupon more than once with MemberPress
<?php
function custom_validate_coupon($errors) {
global $wpdb;
//If user is not logged in, then they've never used a coupon before
if(!MeprUtils::is_user_logged_in()) { return $errors; }
$coupon_code = (isset($_POST['mepr_coupon_code']) && !empty($_POST['mepr_coupon_code']))?stripslashes($_POST['mepr_coupon_code']):'';
if(($coupon = MeprCoupon::get_one_from_code($coupon_code)) && isset($coupon->ID) && $coupon->ID) {
$user = MeprUtils::get_currentuserinfo();
$q = "SELECT COUNT(*) FROM {$wpdb->prefix}mepr_transactions WHERE coupon_id = {$coupon->ID} AND user_id = {$user->ID} AND status in ('complete','confirmed')";
$count = $wpdb->get_var($q);
if($count) {
$errors[] = "This coupon code has already been used";
}
}
return $errors;
}
add_filter('mepr-validate-signup', 'custom_validate_coupon');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment