WC_Product_Data_Store_CPT::get_product_id_by_sku()publicWC 3.0.0

Return product ID based on SKU.

Method of the class: WC_Product_Data_Store_CPT{}

Hooks from the method

Return

Int.

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->get_product_id_by_sku( $sku );
$sku(string) (required)
Product SKU.

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::get_product_id_by_sku() code WC 9.4.2

public function get_product_id_by_sku( $sku ) {
	global $wpdb;

	// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
	$id = $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
			LIMIT 1
			",
			$sku
		)
	);

	return (int) apply_filters( 'woocommerce_get_product_id_by_sku', $id, $sku );
}