You are here

function commerce_product_validate_sku_unique in Commerce Core 7

Checks to see if a given SKU already exists for another product.

Parameters

$sku: The string to match against existing SKUs.

$product_id: The ID of the product the SKU is for; an empty value represents the SKU is meant for a new product.

Return value

TRUE or FALSE indicating whether or not the SKU exists for another product.

File

modules/product/commerce_product.module, line 680
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_validate_sku_unique($sku, $product_id) {

  // Look for an ID of a product matching the supplied SKU.
  if ($match_id = db_query('SELECT product_id FROM {commerce_product} WHERE sku = :sku', array(
    ':sku' => $sku,
  ))
    ->fetchField()) {

    // If this SKU is supposed to be for a new product or a product other than
    // the one that matched...
    if (empty($product_id) || $match_id != $product_id) {
      return FALSE;
    }
  }
  return TRUE;
}