Skip to content

Instantly share code, notes, and snippets.

@dannyconnolly
Created September 24, 2014 15:52
Show Gist options
  • Save dannyconnolly/c5066e86991151711d7b to your computer and use it in GitHub Desktop.
Save dannyconnolly/c5066e86991151711d7b to your computer and use it in GitHub Desktop.
How to Find Product by SKU in WooCommerce - http://www.skyverge.com/blog/find-product-sku-woocommerce/
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment