You are here

function commerce_discount_usage_get_usage_by_mail in Commerce Discount 7

Get usage of a discount for a user, excluding a certain order id.

Parameters

string $discount_name: The discount name.

string $mail: The user mail.

int|bool $exclude_order_id: If provided, the order_id to ignore when calculating the discount usage.

Return value

int Return the number of usage by mail.

1 call to commerce_discount_usage_get_usage_by_mail()
commerce_discount_usage_max_usage_per_person in ./commerce_discount.rules.inc
Rules condition callback: evaluate maximum usage per-person of a discount.

File

./commerce_discount.module, line 1077
Defines the discount and discount offer entities, bundles and functionality.

Code

function commerce_discount_usage_get_usage_by_mail($discount_name, $mail, $exclude_order_id = FALSE) {
  $usage =& drupal_static(__FUNCTION__, array());
  if (!isset($usage[$mail])) {
    $usage[$mail] = array();
    $query = db_select('commerce_discount_usage', 'g')
      ->fields('g')
      ->condition('g.mail', $mail);
    $results = $query
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    foreach ($results as $result) {
      $usage[$mail] += array(
        $result['discount'] => array(),
      );
      $usage[$mail][$result['discount']][] = $result['order_id'];
    }
  }

  // The usage array for the given discount must be initialized if not set,
  // otherwise the array_diff will get a wrong argument.
  $usage[$mail] += array(
    $discount_name => array(),
  );

  // Exclude the order_id passed if necessary.
  if ($exclude_order_id) {
    $usage[$mail][$discount_name] = array_diff($usage[$mail][$discount_name], array(
      $exclude_order_id,
    ));
  }
  return count($usage[$mail][$discount_name]);
}