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