You are here

function hook_uc_coupon_validate in Ubercart Discount Coupons 6

Same name and namespace in other branches
  1. 7.3 uc_coupon.api.php \hook_uc_coupon_validate()
  2. 7.2 uc_coupon.api.php \hook_uc_coupon_validate()

hook_uc_coupon_validate()

Allows modules to influence whether a coupon should be considered valid.

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 calculated 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.

1 function implements hook_uc_coupon_validate()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

uc_coupon_uc_coupon_validate in ./uc_coupon.module
Implementation of hook_uc_coupon_validate().
1 invocation of hook_uc_coupon_validate()
uc_coupon_validate in ./uc_coupon.module
Validate a coupon, and optionally calculate the order discount.

File

./uc_coupon.api.php, line 144
Ubercart Discount Coupon module api/hooks. Version 6.x-1.x

Code

function hook_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.');
      }
    }
  }
}