Introduces woocommerce_get_product_id_by_sku filter

Closes #16559
This commit is contained in:
Claudio Sanches 2017-08-22 15:04:21 -03:00
parent 6c2a944b74
commit 03a189726e
2 changed files with 7 additions and 6 deletions

View File

@ -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. * Return product ID based on SKU.
* *
* @since 3.0.0 * @since 3.0.0
* @param string $sku * @param string $sku Product SKU.
* @return int * @return int
*/ */
public function get_product_id_by_sku( $sku ) { public function get_product_id_by_sku( $sku ) {
global $wpdb; global $wpdb;
return $wpdb->get_var( $wpdb->prepare( "
$id = $wpdb->get_var( $wpdb->prepare( "
SELECT posts.ID SELECT posts.ID
FROM $wpdb->posts AS posts FROM $wpdb->posts AS posts
LEFT JOIN $wpdb->postmeta AS postmeta ON ( posts.ID = postmeta.post_id ) 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' AND postmeta.meta_value = '%s'
LIMIT 1 LIMIT 1
", $sku ) ); ", $sku ) );
return (int) apply_filters( 'woocommerce_get_product_id_by_sku', $id, $sku );
} }
/** /**

View File

@ -560,14 +560,12 @@ function wc_product_generate_unique_sku( $product_id, $sku, $index = 0 ) {
* Get product ID by SKU. * Get product ID by SKU.
* *
* @since 2.3.0 * @since 2.3.0
* @param string $sku * @param string $sku Product SKU.
* @return int * @return int
*/ */
function wc_get_product_id_by_sku( $sku ) { function wc_get_product_id_by_sku( $sku ) {
$data_store = WC_Data_Store::load( 'product' ); $data_store = WC_Data_Store::load( 'product' );
$product_id = $data_store->get_product_id_by_sku( $sku ); return $data_store->get_product_id_by_sku( $sku );
return ( $product_id ) ? intval( $product_id ) : 0;
} }
/** /**