WC_Product_Data_Store_CPT::is_existing_sku( int $product_id, string $sku )
Check if product sku is found for any other product IDs.
Description Description
Parameters Parameters
- $product_id
-
(Required) Product ID.
- $sku
-
(Required) Will be slashed to work around <a href="https://core.trac.wordpress.org/ticket/27421">https://core.trac.wordpress.org/ticket/27421</a>.
Return Return
(bool)
Source Source
File: includes/data-stores/class-wc-product-data-store-cpt.php
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
)
);
}
Changelog Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |