wc_product_generate_unique_sku

Recursively appends a suffix until a unique SKU is found.

Description

(string) wc_product_generate_unique_sku( (integer) $product_id, (string) $sku, (int) $index = 0 ); 

Returns (string)

Parameters (3)

0. $product_id (integer)
The product id.
1. $sku (string)
The sku.
2. $index — Optional. (int)
The index.

Usage

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

Defined (1)

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

/includes/wc-product-functions.php  
  1. function wc_product_generate_unique_sku( $product_id, $sku, $index = 0 ) { 
  2. $generated_sku = 0 < $index ? $sku . '-' . $index : $sku; 
  3.  
  4. if ( ! wc_product_has_unique_sku( $product_id, $generated_sku ) ) { 
  5. $generated_sku = wc_product_generate_unique_sku( $product_id, $sku, ( $index + 1 ) ); 
  6.  
  7. return $generated_sku;