diff --git a/plugins/woocommerce/changelog/46763-dev-46759-wc-product-pre-has-unique-sku b/plugins/woocommerce/changelog/46763-dev-46759-wc-product-pre-has-unique-sku new file mode 100644 index 00000000000..1bbcebdfa2b --- /dev/null +++ b/plugins/woocommerce/changelog/46763-dev-46759-wc-product-pre-has-unique-sku @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Added the `wc_product_pre_has_unique_sku` filter hook to allow SKU uniqueness to be determined externally \ No newline at end of file diff --git a/plugins/woocommerce/includes/wc-product-functions.php b/plugins/woocommerce/includes/wc-product-functions.php index 9ae253d504b..c457cc368c9 100644 --- a/plugins/woocommerce/includes/wc-product-functions.php +++ b/plugins/woocommerce/includes/wc-product-functions.php @@ -615,6 +615,20 @@ function wc_get_product_types() { * @return bool */ function wc_product_has_unique_sku( $product_id, $sku ) { + /** + * Gives plugins an opportunity to verify SKU uniqueness themselves. + * + * @since 9.0.0 + * + * @param bool|null $has_unique_sku Set to a boolean value to short-circuit the default SKU check. + * @param int $product_id The ID of the current product. + * @param string $sku The SKU to check for uniqueness. + */ + $has_unique_sku = apply_filters( 'wc_product_pre_has_unique_sku', null, $product_id, $sku ); + if ( ! is_null( $has_unique_sku ) ) { + return boolval( $has_unique_sku ); + } + $data_store = WC_Data_Store::load( 'product' ); $sku_found = $data_store->is_existing_sku( $product_id, $sku );