commerce_product_replace_sku_tokens

  1. commerce
    1. 7
Versions
7 commerce_product_replace_sku_tokens($sku, $product)

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.

Code

modules/product/commerce_product.module, line 615

<?php
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;
}
?>