How to Find Product by SKU in WooCommerce

Searching the Admin

You can search for a product by SKU in the admin Products list page by starting the search string with the special “SKU:” indicator. So for instance, if the product you are searching for has a SKU of ’4′, your search string would look like: “SKU:4″. Here’s an example:

Programmatically

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:


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;

}