You are here

function uc_coupon_validate in Ubercart Discount Coupons 5

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

Validate a coupon and calculate the coupon amount against the current cart contents.

Parameters

$code: The coupon code entered at the checkout screen

Return value

Returns a coupon result object with details about the validation

2 calls to uc_coupon_validate()
uc_checkout_pane_coupon in ./uc_coupon.module
Checkout Pane callback function.
uc_coupon_checkout_apply in ./uc_coupon.module
Checkout pane AJAX callback.

File

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

Code

function uc_coupon_validate($code) {
  global $user;
  $result->valid = FALSE;
  $code = strtoupper($code);
  $coupon = uc_coupon_find($code);
  if (!$coupon) {
    $result->message = t('This coupon code is invalid or has expired.');
    return $result;
  }
  if (isset($coupon->data['products']) || isset($coupon->data['skus']) || isset($coupon->data['terms']) || isset($coupon->data['product_types'])) {

    // Product coupons apply to the subtotal and quantity of matching products.
    foreach (uc_cart_get_contents() as $item) {
      $cart_total += $item->price * $item->qty;
      $terms = array();
      $query = db_query("SELECT tid FROM {term_node} WHERE nid = %d", $item->nid);
      while ($row = db_fetch_object($query)) {
        $terms[] = $row->tid;
      }
      if (isset($coupon->data['products']) && (isset($coupon->data['negate_products']) xor in_array($item->nid, $coupon->data['products']))) {
        $applicable_total += $item->price * $item->qty;
        $applicable_qty += $item->qty;
      }
      else {
        if (isset($coupon->data['skus']) && in_array($item->model, $coupon->data['skus'])) {
          $applicable_total += $item->price * $item->qty;
          $applicable_qty += $item->qty;
        }
        else {
          if (isset($coupon->data['terms']) && (isset($coupon->data['negate_terms']) xor count(array_intersect($terms, $coupon->data['terms'])))) {
            $applicable_total += $item->price * $item->qty;
            $applicable_qty += $item->qty;
          }
          else {
            if (isset($coupon->data['product_types'])) {
              $type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $item->nid));
              if (in_array($type, $coupon->data['product_types'])) {
                $applicable_total += $item->price * $item->qty;
                $applicable_qty += $item->qty;
              }
            }
          }
        }
      }
    }
  }
  else {

    // Standard coupons apply once to the whole cart.
    foreach (uc_cart_get_contents() as $item) {
      $cart_total += $item->price * $item->qty;
    }
    $applicable_total = $cart_total;
    $applicable_qty = 1;
  }
  if ($applicable_total == 0) {
    $result->message = t('You do not have applicable products in your cart.');
    return $result;
  }

  //  CHECK MAX USES
  if ($coupon->max_uses > 0) {
    $used = db_result(db_query("SELECT COUNT(*) FROM {uc_coupons_orders} AS uco LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id WHERE uos.weight > 0 AND uco.cid = %d AND uco.code = '%s'", $coupon->cid, $code));
    if ($used >= $coupon->max_uses) {
      $result->message = t('This coupon has reached the maximum redemption limit.');
      return $result;
    }
  }

  //  CHECK MAX USES PER USER
  if (isset($coupon->data['max_uses_per_user'])) {
    $used = db_result(db_query("SELECT COUNT(*) FROM {uc_coupons_orders} AS uco LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id WHERE uos.weight > 0 AND uco.cid = %d AND uo.uid = %d", $coupon->cid, $user->uid));
    if ($used >= $coupon->data['max_uses_per_user']) {
      $result->message = t('This coupon has reached the maximum redemption limit.');
      return $result;
    }
  }

  //  CHECK MINIMUM PURCHASE VALUE
  if ($coupon->minimum_order > 0 && $coupon->minimum_order > $cart_total) {
    $result->message = t('You have not reached the minimum order total for this coupon.');
    return $result;
  }

  //  CHECK USER ID
  if (isset($coupon->data['users'])) {
    if (!in_array("{$user->uid}", $coupon->data['users'], TRUE)) {
      $result->message = t('Your user ID is not allowed to use this coupon.');
      return $result;
    }
  }

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

  // CHECK USER PERMISSIONS
  // 1 - both wholesale and retail any user
  // 2 - wholesale only -> users with 'coupon wholesale pricing'
  // 3 - retail only -> users without 'coupon wholesale pricing'
  if ($coupon->data['wholesale'] > 1) {
    if ($coupon->data['wholesale'] == 2) {
      if (!user_access('coupon wholesale pricing')) {
        $result->message = t('You do not have the correct permission to use this coupon.');
        return $result;
      }
    }
    else {
      if ($coupon->data['wholesale'] == 3) {
        if (user_access('coupon wholesale pricing')) {
          $result->message = t('You do not have the correct permission to use this coupon.');
          return $result;
        }
      }
    }
  }
  $result->valid = TRUE;
  $result->code = $coupon->code;
  $result->cid = $coupon->cid;
  $result->title = t('Coupon: @code', array(
    '@code' => $coupon->code,
  ));
  if ($coupon->type == 'percentage') {
    $result->amount = $applicable_total * $coupon->value / 100;
  }
  else {
    if ($coupon->type == 'price') {
      $result->amount = min($applicable_total, $applicable_qty * $coupon->value);
    }
  }
  return $result;
}