Last active
April 17, 2024 13:28
-
-
Save giacomolanzi/d65969c9665ac1107d2f179d66062fd1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 the user is not logged in, they have never used a coupon | |
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']) : ''; | |
// Define the coupon codes here for which you want to perform the check | |
$specific_coupon_codes = ['CODE1', 'CODE2', 'CODE3']; | |
if(in_array($coupon_code, $specific_coupon_codes)) { | |
// The coupon code is one of those specified for the check | |
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