You are here

function commerce_discount_usage_max_usage in Commerce Discount 7

Rules condition callback: evaluate maximum usage of a discount.

1 string reference to 'commerce_discount_usage_max_usage'
commerce_discount_commerce_discount_rule_build in ./commerce_discount.module
Implements hook_commerce_discount_rule_build().

File

./commerce_discount.rules.inc, line 1556
Rules integration for the Commerce Discount module.

Code

function commerce_discount_usage_max_usage($order, $discount_name) {
  if (!($discount = entity_load_single('commerce_discount', $discount_name))) {
    return FALSE;
  }
  $limit = FALSE;

  // Don't use the wrapper getter on purpose for performance reasons.
  if (isset($discount->discount_usage_limit[LANGUAGE_NONE])) {
    $limit = $discount->discount_usage_limit[LANGUAGE_NONE][0]['value'];
  }
  if (!$limit) {
    return TRUE;
  }

  // Find other orders that have same discount.
  $usage = commerce_discount_usage_get_usage($discount_name, $order->order_id);
  return $usage < $limit;
}