Posted on 1 Comment

Coupons – One use per Member

The other day I needed a way to limit coupon use to one per customer.

I thought I would share the code snippet I found and explain how it works below. You can copy the PHP below and paste it into a plugin like Code Snippets.

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');

The code above will check if the member used a coupon code at signup. If so, it then checks to see if the user has had any other completed signups with that same coupon ID.

If not, they’re allowed to signup.

If so, then they see the message “This coupon has already been used”.

Hope this helps someone out there!

1 thought on “Coupons – One use per Member

  1. Really helpful, thank you so much!!!

Leave a Reply

Your email address will not be published. Required fields are marked *