You are here

function uc_coupon_uc_coupon_validate in Ubercart Discount Coupons 6

Same name and namespace in other branches
  1. 7.3 uc_coupon.module \uc_coupon_uc_coupon_validate()
  2. 7.2 uc_coupon.module \uc_coupon_uc_coupon_validate()

Implementation of hook_uc_coupon_validate().

Parameters

$coupon: The coupon object to validate, with special fields set as follows:

  • $coupon->code: The specific code to be applied (even for bulk coupons).
  • $coupon->amount: If $order !== FALSE, the discount that should be applied.
  • $coupon->usage: Coupon usage data from uc_coupon_count_usage().

$order: The order against which this coupon is to be applied, or FALSE to bypass order validation.

$account: The account of the user trying to use the coupon, or FALSE to bypass user validation.

Return value

TRUE if the coupon should be accepted. NULL to allow other modules to determine validation. Otherwise, a string describing the reason for failure.

File

./uc_coupon.module, line 552
Provides discount coupons for Ubercart.

Code

function uc_coupon_uc_coupon_validate(&$coupon, $order, $account) {

  // Check maximum usage per code.
  if ($coupon->max_uses > 0 && $coupon->usage['codes'][$coupon->code] >= $coupon->max_uses) {
    return t('This coupon has reached the maximum redemption limit.');
  }

  // Check maximum usage per user.
  if ($account && isset($coupon->data['max_uses_per_user']) && $coupon->usage['user'] >= $coupon->data['max_uses_per_user']) {
    return t('This coupon has reached the maximum redemption limit.');
  }

  // Check user ID.
  if ($account && isset($coupon->data['users'])) {
    if (in_array("{$account->uid}", $coupon->data['users'], TRUE) xor !isset($coupon->data['negate_users'])) {
      return t('Your user ID is not allowed to use this coupon.');
    }
  }

  // Check roles.
  if ($account && isset($coupon->data['roles'])) {
    $role_found = FALSE;
    foreach ($coupon->data['roles'] as $role) {
      if (in_array($role, $account->roles)) {
        $role_found = TRUE;
        break;
      }
    }
    if ($role_found xor !isset($coupon->data['negate_roles'])) {
      return t('You do not have the correct permission to use this coupon.');
    }
  }

  // Check wholesale permissions.
  if ($account) {
    if ($coupon->data['wholesale'] == 2 && !user_access('coupon wholesale pricing', $account)) {
      return t('You do not have the correct permission to use this coupon.');
    }
    else {
      if ($coupon->data['wholesale'] == 3 && user_access('coupon wholesale pricing', $account)) {
        return t('You do not have the correct permission to use this coupon.');
      }
    }
  }
}