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


Top ↑

Return Return

(bool)


Top ↑

Source Source

File: includes/data-stores/class-wc-product-data-store-cpt.php

980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
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
        )
    );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.