WC_Product_Data_Store_CPT::is_existing_sku()publicWC 3.0.0

Check if product sku is found for any other product IDs.

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Return

true|false.

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->is_existing_sku( $product_id, $sku );
$product_id(int) (required)
Product ID.
$sku(string) (required)
Will be slashed to work around https://core.trac.wordpress.org/ticket/27421.

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::is_existing_sku() code WC 9.4.2

public function is_existing_sku( $product_id, $sku ) {
	global $wpdb;

	// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
	return (bool) $wpdb->get_var(
		$wpdb->prepare(
			"
			SELECT posts.ID
			FROM {$wpdb->posts} as posts
			INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id
			WHERE
			posts.post_type IN ( 'product', 'product_variation' )
			AND posts.post_status != 'trash'
			AND lookup.sku = %s
			AND lookup.product_id <> %d
			LIMIT 1
			",
			wp_slash( $sku ),
			$product_id
		)
	);
}