You are here

function uc_coupon_count_usage in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 6 uc_coupon.module \uc_coupon_count_usage()
  2. 7.2 uc_coupon.module \uc_coupon_count_usage()

Count usage of a coupon.

Parameters

$cid: The coupon id to count.

$uid: (optional) The user id to count. Defaults to the current user.

array $exclude_oids: (optional) If supplied, will exclude usage for the specified order ids.

Return value

An associative array containing:

  • codes: An associative array of code => usage count.
  • user: The usage count by the specified (or current) user.
5 calls to uc_coupon_count_usage()
uc_coupon_add_form in ./uc_coupon.admin.inc
Coupon add/edit form.
uc_coupon_handler_field_codes::pre_render in views/uc_coupon_handler_field_codes.inc
Expand the coupon codes for each coupon in the result set.
uc_coupon_purchase_view in uc_coupon_purchase/uc_coupon_purchase.pages.inc
Display a list of purchased coupons.
uc_coupon_tokens in ./uc_coupon.tokens.inc
Implements hook_tokens().
uc_coupon_validate in ./uc_coupon.module
Validate a coupon, and optionally calculate the order discount.

File

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

Code

function uc_coupon_count_usage($cid, $uid = NULL, $exclude_oids = array()) {
  global $user;
  $weight = uc_order_status_data(variable_get('uc_coupon_used_order_status', 'processing'), 'weight');
  $usage = array(
    'codes' => array(),
    'value' => array(
      'codes' => array(),
    ),
  );
  $exclude_where = empty($exclude_oids) ? '' : 'AND uo.order_id NOT IN (:oids)';
  $result = db_query("SELECT uco.code, COUNT(*) AS uses, SUM(uco.value) AS value FROM {uc_coupons_orders} AS uco\n    LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id\n    LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id\n    WHERE uos.weight >= :weight AND uco.cid = :cid {$exclude_where} GROUP BY uco.code", array(
    ':weight' => $weight,
    ':cid' => $cid,
    ':oids' => $exclude_oids,
  ));
  foreach ($result as $row) {
    $usage['codes'][$row->code] = $row->uses;
    $usage['value']['codes'][$row->code] = $row->value;
  }
  if (is_null($uid)) {
    $uid = $user->uid;
  }
  $usage['user'] = db_query("SELECT COUNT(*) FROM {uc_coupons_orders} AS uco\n    LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id\n    LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id\n    WHERE uos.weight >= :weight AND uco.cid = :cid AND uo.uid = :uid", array(
    ':weight' => $weight,
    ':cid' => $cid,
    ':uid' => $uid,
  ))
    ->fetchField();

  // Allow other modules to implement usage counts.
  drupal_alter('uc_coupon_usage', $usage, $cid, $uid);
  return $usage;
}