You are here

function uc_coupon_validate in Ubercart Discount Coupons 6

Same name and namespace in other branches
  1. 5 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 optionally calculate the order discount.

Parameters

$code: The coupon code entered at the checkout screen.

$order: The order that the coupon is being applied to. If NULL, the current cart contents will be used. If FALSE, product and order validation will be bypassed.

$account: The user who is attempting to use the coupon. If NULL, the current user will be assumed. If FALSE, user validation will be bypassed.

Return value

A coupon object with extended information about the validation:

  • $coupon->valid: TRUE if the code was valid, FALSE otherwise.
  • $coupon->code: The specific code to be applied (even for bulk coupons).
  • $coupon->title: The line item title for the discount.
  • $coupon->amount: If $order !== FALSE, the discount that should be applied.
  • $coupon->message: If $coupon->valid == FALSE, the rejection reason.
4 calls to uc_coupon_validate()
uc_checkout_pane_coupon in ./uc_coupon.module
Checkout Pane callback function.
uc_coupon_order in ./uc_coupon.module
Implementation of hook_order().
uc_coupon_submit in ./uc_coupon.module
Submit a coupon from checkout page or block.
uc_coupon_uc_cart_alter in ./uc_coupon.module
Implementation of hook_uc_cart_alter().

File

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

Code

function uc_coupon_validate($code, $order = NULL, $account = NULL) {
  global $user;
  if (is_null($order)) {
    $order = new stdClass();
    $order->products = uc_cart_get_contents();
  }
  if (is_null($account)) {
    $account = $user;
  }

  // Look for an active coupon matching the code.
  $code = trim(strtoupper($code));
  $coupon = uc_coupon_find($code);
  if (!$coupon) {
    $coupon = new stdClass();
    $coupon->valid = FALSE;
    $coupon->message = t('This coupon code is invalid or has expired.');
    return $coupon;
  }

  // Assume the coupon is valid, unless a validation hook fails.
  $coupon->code = $code;
  $coupon->valid = TRUE;
  $coupon->usage = uc_coupon_count_usage($coupon->cid, $account ? $account->uid : NULL);

  // Calculate the discount.
  // We do this before invoking the validate hook so that modules can use this information.
  if ($order) {
    $order->data['coupon'] = $code;
    $items = uc_coupon_calculate_discounts($coupon, $order);
    $coupon->amount = 0;

    // If an array of items was returned, then sum the discounts for each applicable item.
    if (is_array($items)) {
      foreach ($items as $item) {
        $coupon->amount += $item->discount;
      }
    }
    else {
      $coupon->valid = FALSE;
      $coupon->message = $items;
    }
    $coupon->amount = round($coupon->amount, variable_get('uc_currency_prec', 2));
  }

  // Invoke validation hook.
  foreach (module_implements('uc_coupon_validate') as $module) {
    $callback = $module . '_uc_coupon_validate';
    $result = $callback($coupon, $order, $account);
    if ($result === TRUE) {

      // This module wishes the coupon to be accepted.
      $coupon->valid = TRUE;
    }
    else {
      if (!is_null($result)) {

        // This module wishes the coupon to be rejected.
        $coupon->valid = FALSE;
        $coupon->message = $result;
      }
    }
  }

  // Create the line item title for this coupon.
  $format = !empty($coupon->data['line_item_format']) ? $coupon->data['line_item_format'] : variable_get('uc_coupon_line_item_format', t('Coupon: [coupon-code]'));
  $coupon->title = token_replace_multiple(check_plain($format), array(
    'coupon' => $coupon,
  ));

  // Set a custom message if one exists.
  if ($coupon->valid && empty($coupon->message) && !empty($coupon->data['apply_message'])) {
    $coupon->message = token_replace_multiple(check_plain($coupon->data['apply_message']), array(
      'coupon' => $coupon,
    ));
  }
  return $coupon;
}