You are here

function hook_uc_coupon_validate in Ubercart Discount Coupons 7.3

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

Add extra validation to a coupon.

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.

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
Implements 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 167
Ubercart Discount Coupon module api/hooks. Version 7.x-2.x

Code

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

  // Check for allowed combinations.
  if (!empty($order->data['coupons'])) {
    foreach (array_keys($order->data['coupons']) as $code) {
      $other = uc_coupon_find($code);
      $other_listed = !empty($coupon->data['combinations']) && in_array($other->cid, $coupon->data['combinations']);
      $this_ok = (isset($coupon->data['negate_combinations']) xor $other_listed);
      $this_listed = !empty($other->data['combinations']) && in_array($coupon->cid, $other->data['combinations']);
      $other_ok = (isset($other->data['negate_combinations']) xor $this_listed);
      if (!$this_ok || !$other_ok) {
        return t('This coupon combination is not allowed.');
      }
    }
  }

  // Check maximum usage per code.
  if ($coupon->max_uses > 0 && !empty($coupon->usage['codes'][$coupon->code]) && $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.');
    }
  }
}