Forked from cartpauj/memberpress-use-coupon-once-per-user.php
Created
February 2, 2022 09:34
-
-
Save jasperf/69024401cb38458b84242c1951b4742f to your computer and use it in GitHub Desktop.
User cannot use coupon more than once with MemberPress
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 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