Add filter hook wc_product_pre_has_unique_sku (#46763)

Adds the `wc_product_pre_has_unique_sku` filter hook to allow SKU uniqueness to be determined externally in order to avoid an expensive query.

Fixes #46759
Fixes #31720

---------

Co-authored-by: Corey McKrill <916023+coreymckrill@users.noreply.github.com>
This commit is contained in:
Alexander Minza 2024-04-25 20:41:33 +03:00 committed by GitHub
parent f7cad9c4be
commit ac7bcdbab3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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 );