You are here

function commerce_product_replace_sku_tokens in Commerce Core 7

Performs token replacement on a SKU for valid tokens only.

TODO: This function currently limits acceptable Tokens to Product ID and type with no ability to use Tokens for the Fields attached to the product. 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

$sku: The raw SKU string including any tokens as entered.

$product: A product object used to perform token replacement on the SKU.

Return value

The SKU with tokens replaced or else FALSE if it included invalid tokens.

File

modules/product/commerce_product.module, line 640
Defines the core Commerce product entity, including the entity itself, the bundle definitions (product types), and various API functions to manage products and interact with them through forms and autocompletes.

Code

function commerce_product_replace_sku_tokens($sku, $product) {

  // Build an array of valid SKU tokens.
  $valid_tokens = array(
    'product-id',
    'type',
  );

  // Ensure that only valid tokens were used.
  $invalid_tokens = FALSE;
  foreach (token_scan($sku) as $type => $token) {
    if ($type !== 'product') {
      $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 $sku;
}