You are here

function commerce_order_compare_order_amount_build in Commerce Discount 7

Build callback for commerce_order_compare_order_amount.

Parameters

EntityDrupalWrapper $wrapper: The wrapped entity given by the rule.

string $operator: The comparison operator.

array $total: A commerce_price type array.

Return value

bool return true if condition is valid. false otherwise.

1 string reference to 'commerce_order_compare_order_amount_build'
commerce_discount_inline_conditions_info in ./commerce_discount.inline_conditions.inc
Implements hook_inline_conditions_info().

File

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

Code

function commerce_order_compare_order_amount_build(EntityDrupalWrapper $wrapper, $operator, $total, $line_item_types) {
  $total_order = 0;

  // Ensure the discount currency code is the same as the order.
  if ($wrapper->commerce_order_total->currency_code
    ->value() != $total['currency_code']) {
    return FALSE;
  }

  // If $line_item_types is not an array, then we need to total all line item
  // types to preserve backwards compatibility.
  if (!is_array($line_item_types)) {
    $line_item_types = commerce_order_compare_order_amount_options_default();
  }

  // Get given total order amount.
  foreach ($wrapper->commerce_line_items as $line_item_wrapper) {
    if (in_array($line_item_wrapper
      ->getBundle(), $line_item_types, TRUE)) {

      // Convert the line item's total to the order's currency for totalling.
      $component_total = commerce_price_component_total($line_item_wrapper->commerce_total
        ->value());

      // Add the totals.
      $total_order += commerce_currency_convert($component_total['amount'], $component_total['currency_code'], $total['currency_code']);
    }
  }
  switch ($operator) {
    case '<':
      return $total_order < $total['amount'];
    case '<=':
      return $total_order <= $total['amount'];
    case '==':
      return $total_order == $total['amount'];
    case '>':
      return $total_order > $total['amount'];
    case '>=':
      return $total_order >= $total['amount'];
    default:
      return FALSE;
  }
}