How to Get Product ID From Variation SKU in WooCommerce (Expert Guide)

How To Find Product Variations using Product Id WooCommerce
1500 Views
3.6
(79)

In this guide, we’ll show you how to work with product attributes and variations in WooCommerce.

WooCommerce has a great feature that lets store owners easily set up products that have different options like size or color. This means customers can choose options like t-shirt size directly from the product page, without having to look at multiple different shirts.

However, sometimes you may need to change these options or add new ones to make shopping even better for your customers. This guide will show you how to do this, how to get, change and show different product options.

We’ll tell you everything you need to know about using product options in your WooCommerce store.

let’s get started!

Overview

In this guide, I’ll show you how to find a WooCommerce product or its variation using just the SKU. Normally, if you have the product ID, you can easily find the product with the wc_get_product() function. However, this function doesn’t work with SKUs.

I’ll explain three simple methods to help you find a product using its SKU. We’ll look at two methods that use WooCommerce tools (WC_Product_Query and wc_get_product_id_by_sku()) and one method that uses a WordPress tool (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 the examples I’ve mentioned, I use the reset() function to access the first element of an array. This serves a similar purpose to directly referencing the first item with $products[0]. This approach helps simplify accessing the initial item in a list of products.

Get Product or Variation ID by SKU with wc_get_product_id_by_sku() 

Here’s a simpler explanation: The function wc_get_product_id_by_sku() is a handy tool that helps you find the ID of a product or its variation using the 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 

When using WooCommerce, it’s best to use its own functions and methods to manage products and orders. This way, you make sure to use any built-in security features and speed things up with caching.

The SKU, or product code, is a special piece of information for each product. It’s saved in the wp_postmeta table in the database with the label _sku.

$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;

You can see how complex the solution becomes, right? Switching from WP_Query to the get_posts() function doesn’t simplify things either.

However, there’s a positive aspect it works great 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 

When you want to find products by their SKU using the WooCommerce REST API, you use the /wc/v3/products endpoint. Simply include sku as a parameter in your request. For 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 the $key and $secret, you can either use an application password or create a consumer key and consumer secret in the WooCommerce settings.

Read Next:- How to Disable AJAX Cart Fragments in WooCommerce

How useful was this blog?

Click on a star to rate it!

Average rating 3.6 / 5. Vote count: 79

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *