WC_Product_Data_Store_CPT::get_product_id_by_sku()publicWC 3.0.0

Return product ID based on SKU.

{} Это метод класса: WC_Product_Data_Store_CPT{}

Хуки из метода

Возвращает

int.

Использование

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->get_product_id_by_sku( $sku );
$sku(строка) (обязательный)
Product SKU.

Список изменений

С версии 3.0.0 Введена.

Код WC_Product_Data_Store_CPT::get_product_id_by_sku() WC 6.4.1

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