Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save giacomolanzi/d65969c9665ac1107d2f179d66062fd1 to your computer and use it in GitHub Desktop.
Save giacomolanzi/d65969c9665ac1107d2f179d66062fd1 to your computer and use it in GitHub Desktop.
<?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