function uc_coupon_validate in Ubercart Discount Coupons 7.3
Same name and namespace in other branches
- 5 uc_coupon.module \uc_coupon_validate()
- 6 uc_coupon.module \uc_coupon_validate()
- 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->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.
3 calls to uc_coupon_validate()
- uc_coupon_form_submit in ./
uc_coupon.module - Submit handler for the uc_coupon form.
- 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.
- uc_coupon_validate_multiple in ./
uc_coupon.module - Validates a list of coupon codes against a specified order and account.
File
- ./
uc_coupon.module, line 656 - Provides discount codes and gift certificates 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.');
$coupon->title = t('Unknown');
return $coupon;
}
// Count usage for this coupon.
$uid = !empty($account) ? $account->uid : NULL;
// If the order exists, don't count it towards coupon usage.
$oids = !empty($order->order_id) ? array(
$order->order_id,
) : array();
$coupon->usage = uc_coupon_count_usage($coupon->cid, $uid, $oids);
// Calculate the discounts (if any).
uc_coupon_prepare($coupon, $code, uc_coupon_calculate_discounts($coupon, $order));
// 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 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;
}