Skip to content

Commit

Permalink
Improved the wc_get_product_id_by_sku() function for woocommerce#7695
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiosanches committed Mar 12, 2015
1 parent 607d5e9 commit a66d627
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions includes/wc-product-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*
* Functions for product specific things.
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @version 2.1.0
* @author WooThemes
* @category Core
* @package WooCommerce/Functions
* @version 2.3.0
*/

/**
Expand Down Expand Up @@ -539,13 +539,14 @@ function wc_product_has_unique_sku( $product_id, $sku ) {
function wc_get_product_id_by_sku( $sku ) {
global $wpdb;

$product_id = $wpdb->get_var( $wpdb->prepare("
SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
WHERE $wpdb->posts.post_type IN ( 'product', 'product_variation' )
AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s'
LIMIT 1", $sku));
$product_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 )
WHERE posts.post_type IN ( 'product', 'product_variation' )
AND postmeta.meta_key = '_sku' AND postmeta.meta_value = '%s'
LIMIT 1
", $sku ) );

return ( $product_id ) ? intval( $product_id ) : 0;
}

0 comments on commit a66d627

Please sign in to comment.