You are here

function uc_coupon_validate in Ubercart Discount Coupons 7.2

Same name and namespace in other branches
  1. 5 uc_coupon.module \uc_coupon_validate()
  2. 6 uc_coupon.module \uc_coupon_validate()
  3. 7.3 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->message: A message to be displayed accepting the acceptance or rejection of this coupon.
  • $coupon->amount: If $order !== FALSE, the discount that should be applied.
  • $coupon->discounts: if $order !== FALSE, an array discounts on individual products indexed by nid, containing the following fields: -> 'discount' = The full value of the discount on that item. -> 'pretax_discount' => The actual pre-tax discount. For fixed discounts to products with taxes included, we apply the face value of the coupon tax-inclusively also; that is, the actual discount is calculated so that the face value is correct after taxes.
1 call to uc_coupon_validate()
uc_coupon_session_validate in ./uc_coupon.module
Validates all coupons in the current session. The validated coupons are statically cached for each request. The cache is rebuilt the first time this function is called, or every time the cart contents are rebuilt.

File

./uc_coupon.module, line 615

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.');
    $coupon->title = t('Unknown');
    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);
  unset($coupon->message);

  // Calculate the discount.
  if ($order) {
    $coupon->amount = 0;
    $coupon->pretax_amount = 0;
    $discounts = uc_coupon_calculate_discounts($coupon, $order, TRUE);
    if (!is_array($discounts)) {
      $coupon->discounts = array();
      $coupon->valid = FALSE;
      $coupon->message = $discounts;
    }
    else {
      $coupon->discounts = $discounts;
      foreach ($coupon->discounts as $item) {
        $coupon->amount += $item->discount;
        $coupon->pretax_amount += isset($item->pretax_discount) ? $item->pretax_discount : $item->discount;
      }
      $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;
    }
    elseif (!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 !code', array(
    '!code' => '[uc_coupon:code]',
  )));
  $coupon->title = token_replace(check_plain($format), array(
    'uc_coupon' => $coupon,
  ));

  // Create a success message.
  if ($coupon->valid && !isset($coupon->message)) {
    if (isset($coupon->data['apply_message'])) {
      $coupon->message = token_replace(check_plain($coupon->data['apply_message']), array(
        'uc_coupon' => $coupon,
      ));
    }
    else {
      $amount = theme('uc_price', array(
        'price' => $coupon->amount,
      ));
      if (isset($order) || variable_get('uc_coupon_show_in_cart', TRUE)) {
        $coupon->message = t('A discount of !amount has been applied to your order.', array(
          '!amount' => $amount,
        ));
      }
      else {
        $coupon->message = t('A discount of !amount will be applied at checkout.', array(
          '!amount' => $amount,
        ));
      }
    }
  }
  return $coupon;
}