how to search by sku woocommerce

Solutions on MaxInterview for how to search by sku woocommerce by the best coders in the world

showing results for - "how to search by sku woocommerce"
Melina
08 Oct 2016
1function get_product_by_sku( $sku ) {
2  global $wpdb;
3
4  $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
5
6  if ( $product_id ) return new WC_Product( $product_id );
7
8  return null;
9}
10