You are here

function commerce_order_rules_compare_total_quantity in Commerce Core 7

Condition callback: compares the total quantity of products on an order against the specified quantity.

1 string reference to 'commerce_order_rules_compare_total_quantity'
commerce_order_rules_condition_info in modules/order/commerce_order.rules.inc
Implements hook_rules_condition_info().

File

modules/order/commerce_order.rules.inc, line 416
Rules integration for orders.

Code

function commerce_order_rules_compare_total_quantity($order, $operator, $value) {
  $quantity = 0;

  // If we got a valid order with line items...
  if (!empty($order) && !empty($order->commerce_line_items)) {

    // Collect all the line item IDs on the order.
    $line_item_ids = array();
    if ($items = field_get_items('commerce_order', $order, 'commerce_line_items')) {
      foreach ($items as $item) {
        $line_item_ids[] = $item['line_item_id'];
      }
    }

    // If we found valid line item IDs...
    if (!empty($line_item_ids)) {
      $quantity = commerce_line_items_quantity_by_id($line_item_ids, commerce_product_line_item_types());
    }
  }

  // Make a quantity comparison based on the operator.
  switch ($operator) {
    case '<':
      return $quantity < $value;
    case '<=':
      return $quantity <= $value;
    case '=':
      return $quantity == $value;
    case '>=':
      return $quantity >= $value;
    case '>':
      return $quantity > $value;
  }
  return FALSE;
}