wc_product_has_unique_sku

Check if product sku is unique.

Description

wc_product_has_unique_sku( (int) $product_id, (string) $sku ); 

Parameters (2)

0. $product_id (int)
The product id.
1. $sku (string)
The sku.

Usage

  1. if ( !function_exists( 'wc_product_has_unique_sku' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // The product id. 
  5. $product_id = -1; 
  6.  
  7. // The sku. 
  8. $sku = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_product_has_unique_sku($product_id, $sku); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-product-functions.php  
  1. function wc_product_has_unique_sku( $product_id, $sku ) { 
  2. $data_store = WC_Data_Store::load( 'product' ); 
  3. $sku_found = $data_store->is_existing_sku( $product_id, $sku ); 
  4.  
  5. if ( apply_filters( 'wc_product_has_unique_sku', $sku_found, $product_id, $sku ) ) { 
  6. return false; 
  7. } else { 
  8. return true;