From 03a189726e67a77c780573bd372f0b8c7c2d0185 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Tue, 22 Aug 2017 15:04:21 -0300 Subject: [PATCH] Introduces woocommerce_get_product_id_by_sku filter Closes #16559 --- includes/data-stores/class-wc-product-data-store-cpt.php | 7 +++++-- includes/wc-product-functions.php | 6 ++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index d4a533b6476..46f3f42f5e6 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -846,12 +846,13 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * Return product ID based on SKU. * * @since 3.0.0 - * @param string $sku + * @param string $sku Product SKU. * @return int */ public function get_product_id_by_sku( $sku ) { global $wpdb; - return $wpdb->get_var( $wpdb->prepare( " + + $id = $wpdb->get_var( $wpdb->prepare( " SELECT posts.ID FROM $wpdb->posts AS posts LEFT JOIN $wpdb->postmeta AS postmeta ON ( posts.ID = postmeta.post_id ) @@ -861,6 +862,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da AND postmeta.meta_value = '%s' LIMIT 1 ", $sku ) ); + + return (int) apply_filters( 'woocommerce_get_product_id_by_sku', $id, $sku ); } /** diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 8e124b2de33..4c8a7703e93 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -560,14 +560,12 @@ function wc_product_generate_unique_sku( $product_id, $sku, $index = 0 ) { * Get product ID by SKU. * * @since 2.3.0 - * @param string $sku + * @param string $sku Product SKU. * @return int */ function wc_get_product_id_by_sku( $sku ) { $data_store = WC_Data_Store::load( 'product' ); - $product_id = $data_store->get_product_id_by_sku( $sku ); - - return ( $product_id ) ? intval( $product_id ) : 0; + return $data_store->get_product_id_by_sku( $sku ); } /**