You are here

function commerce_order_replace_number_tokens in Commerce Core 7

Performs token replacement on an order number for valid tokens only.

TODO: This function currently limits acceptable Tokens to Order ID with no ability to use Tokens for the Fields attached to the order. That might be fine for a core Token replacement, but we should at least open the $valid_tokens array up to other modules to enable various Tokens for use.

Parameters

$order_number: The raw order number string including any tokens as entered.

$order: An order object used to perform token replacement on the number.

Return value

The number with tokens replaced or FALSE if it included invalid tokens.

File

modules/order/commerce_order.module, line 929
Defines the core Commerce order entity and API functions to manage orders and interact with them.

Code

function commerce_order_replace_number_tokens($order_number, $order) {

  // Build an array of valid order number tokens.
  $valid_tokens = array(
    'order-id',
  );

  // Ensure that only valid tokens were used.
  $invalid_tokens = FALSE;
  foreach (token_scan($order_number) as $type => $token) {
    if ($type !== 'order') {
      $invalid_tokens = TRUE;
    }
    else {
      foreach (array_keys($token) as $value) {
        if (!in_array($value, $valid_tokens)) {
          $invalid_tokens = TRUE;
        }
      }
    }
  }

  // Register the error if an invalid token was detected.
  if ($invalid_tokens) {
    return FALSE;
  }
  return $order_number;
}