How To Retrieve Product By SKU in WooCommerce (Code Snippet)

WooCommerceBeen a while since I’ve posted here, so I thought I’d drop a little code snippet helper function that you can put in your functions.php or plugin for retrieving a product via the SKU in WooCommerce.

Since Woo doesn’t provide a helper function for retrieving a product via SKU this has come in handy a number of times. Hope it helps you in whatever project you’re facing.

function get_product_by_sku($sku) {
	global $wpdb;
	$product_id = $wpdb->get_var(
			"SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku
	if ($product_id)
		return get_product($product_id);
	return null;

Josh Kohlbach

Josh is a software entrepreneur from Brisbane, Australia. He spends most of his time helping e-commerce store owners. This is his personal blog where he shares his thoughts and other tidbits on online business and life in general.

Leave a Reply