Want to get Product id using product SKU in WooCommerce? Here’s a little snippet of code which you can use in your WooCommerce plugin development to get a particular product by its SKU. Enjoy:
1 2 3 4 5 6 7 8 9 10 | function get_product_by_sku( $sku ) { global $wpdb; $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) ); if ( $product_id ) return new WC_Product( $product_id ); return null; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.