You are here

function uc_coupon_prepare in Ubercart Discount Coupons 7.3

Prepares a coupon for validation and application to an order.

Parameters

$coupon: A raw coupon object.

$discounts: An associative array of the discounts to be applied, keyed by nid or -lid. Or a string containing a message indicating why there are no discounts available.

Return value

A fully validated coupon object with all additional properties set. This is returned for convenience, as the $coupon provided is passed by reference and modified directly.

See also

uc_coupon_validate().

3 calls to uc_coupon_prepare()
uc_coupon_get_order_coupons in ./uc_coupon.module
Gets the fully validated coupon objects that have been applied to this order.
uc_coupon_recurring_recurring_renewal_pending in uc_coupon_recurring/uc_coupon_recurring.module
Implements hook_recurring_renewal_pending().
uc_coupon_validate in ./uc_coupon.module
Validate a coupon, and optionally calculate the order discount.

File

./uc_coupon.module, line 735
Provides discount codes and gift certificates for Ubercart.

Code

function uc_coupon_prepare($coupon, $code, $discounts) {
  $coupon->code = $code;
  $coupon->valid = TRUE;
  $coupon->amount = 0;
  $coupon->pretax_amount = 0;
  if (!is_array($discounts)) {
    $coupon->discounts = array();
    $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));
    unset($coupon->message);
  }

  // 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,
  ));
  return $coupon;
}