Get Product (or Variation) by SKU

In this tutorial I will show you how to get a product or a product variation object in WooCommerce if you only know its SKU. It is easy-peasy when we get a product object by ID, we just have to use wc_get_product() function, but we can not pass SKU to that function.

So I am going to show you three different ways how you can obtain a product object from SKU, we are going to do it both WooCommerce-way (with WP_Product_Query and with wc_get_product_id_by_sku() function) and WordPress-way (with WP_Query).

Get Product by SKU with WC_Product_Query

$query = new WC_Product_Query( array(
    'sku' => 'MISHA-123',
) );
$products = $query->get_products();
$product = reset( $products );

// let's print the product price for example
echo $product->get_price();
// or product ID
echo $product->get_id();

It can be achieved also with wc_get_products() function.

$products = wc_get_products( array( 'sku' => 'MISH-123' ) );
$product = reset( $products );

In all the above examples I am using reset() function to get the first element of array. So it is sort of analogue of using $products[0].

But there is one huge thing – the examples above aren’t going to work with product variations! So, please continue to read this article to find out how to deal with them.

Get Product or Variation ID by SKU with wc_get_product_id_by_sku()

Here it is. Function wc_get_product_id_by_sku() is an universal solution that allows to get an ID of either a product or a product variation by its SKU.

$product_id = wc_get_product_id_by_sku( 'MISH-123' );
if( $product_id ) {
	$product = wc_get_product( $product_id );
} else {
	echo 'Neither a product nor a variation has found.';
}

For variations:

$variation_id = wc_get_product_id_by_sku( 'MISH-234' );
$variation = wc_get_product_object( 'variation', $variation_id );

Get Product by SKU with WP_Query

In order to use another way we have to come to understanding of two things:

  1. When working with WooCommerce in most cases it is better to use its own functions and methods that intended to work with products, orders etc, because when you’re trying to use the WordPress or PHP functions for that, you will definitely bypass caching and maybe some security thingies along the way.
  2. SKU of a products is just a custom field of a post, that is stored in wp_postmeta table under _sku key.
$products = new WP_Query(
	array(
		'post_type'  => array( 'product', 'product_variation' ),
		'meta_query' => array(
			array(
				'key'     => '_sku',
				'value'   => 'MISH-123',
			)
		),
	)
);

while( $products->have_posts() ) : $products->the_post();
	$product = wc_get_product( $products->post->ID );
	echo $product->get_price();
endwhile;

Do you see now how weird and much more complicated solution is this? Even if we replace WP_Query with get_posts() function it doesn’t make the things much better.

But let’s look on the bright side, it works perfectly with product variations.

$products = get_posts(
	array(
		'post_type'  => array( 'product', 'product_variation' ),
		'meta_query' => array(
			array(
				'key'     => '_sku',
				'value'   => 'MISH-123',
			)
		),
	)
);

$post = reset( $products );
$product = wc_get_product( $post->ID );
echo $product->get_price();

Get Product by SKU in WooCommerce REST API

The last but not least let’s talk about getting product by SKU while working with WooCommerce REST API.

In order to do so we have to use /wc/v3/products endpoint and provide sku argument to it. Example:

$sku = 'MISH-123';

$request = wp_remote_get(
	add_query_arg( 'sku', $sku, 'https://your-website/wp-json/wc/v3/products' ),
	array(
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$key:$secret" )
		)
	)
);

if( 'OK' === wp_remote_retrieve_response_message( $request ) ) {
	$products = json_decode( wp_remote_retrieve_body( $request ) );
	if( $products ) {
		$product = reset( $products );
		echo "We found a product with ID {$product->id}";
	} else {
		echo 'No products with this specific SKU have been found';
	}
}

For $key and $secret here you can use an application password or create a consumer key and consumer secret in WooCommerce settings.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter