wc_product_force_unique_sku

Force a unique SKU.

Description

wc_product_force_unique_sku( (integer) $product_id ); 

Parameters (1)

0. $product_id (integer)
The product id.

Usage

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

Defined (1)

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

/includes/wc-product-functions.php  
  1. function wc_product_force_unique_sku( $product_id ) { 
  2. $product = wc_get_product( $product_id ); 
  3.  
  4. if ( $product && ( $current_sku = $product->get_sku( 'edit' ) ) ) { 
  5. try { 
  6. $new_sku = wc_product_generate_unique_sku( $product_id, $current_sku ); 
  7.  
  8. if ( $current_sku !== $new_sku ) { 
  9. $product->set_sku( $new_sku ); 
  10. $product->save(); 
  11. } catch ( Exception $e ) {}