You are here

function uc_coupon_find in Ubercart Discount Coupons 6

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

Load a coupon (single or bulk) from the supplied code.

2 calls to uc_coupon_find()
uc_coupon_tax_adjustment in ./uc_coupon.module
Handle tax on coupons by calculating tax for individual discounted prices. This is currently only supported by the VAT module (uc_vat).
uc_coupon_validate in ./uc_coupon.module
Validate a coupon, and optionally calculate the order discount.

File

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

Code

function uc_coupon_find($code) {

  // Look for matching single coupon first.
  $coupon = db_fetch_object(db_query("SELECT cid FROM {uc_coupons} WHERE code = '%s' AND status = 1 AND bulk = 0 AND valid_from < %d AND (valid_until = 0 OR valid_until > %d)", $code, time(), time()));
  if ($coupon) {
    return uc_coupon_load($coupon->cid);
  }

  // Look through bulk coupons.
  $result = db_query("SELECT cid, code, data, bulk_seed FROM {uc_coupons} WHERE status = 1 AND bulk = 1 AND valid_from < %d AND (valid_until = 0 OR valid_until > %d)", time(), time());
  while ($coupon = db_fetch_object($result)) {

    // Check coupon prefix.
    $prefix_length = strlen($coupon->code);
    if (substr($code, 0, $prefix_length) != $coupon->code) {
      continue;
    }
    if ($coupon->data) {
      $coupon->data = unserialize($coupon->data);
    }

    // Check coupon sequence ID.
    $id = substr($code, $prefix_length, strlen(dechex($coupon->data['bulk_number'])));
    if (!preg_match("/^[0-9A-F]+\$/", $id)) {
      continue;
    }
    $id = hexdec($id);
    if ($id < 0 || $id > $coupon->data['bulk_number']) {
      continue;
    }

    // Check complete coupon code.
    if ($code == uc_coupon_get_bulk_code($coupon, $id)) {
      return uc_coupon_load($coupon->cid);
    }
  }
  return FALSE;
}