Create and Update Products with WooCommerce API

Recently I was publishing and updating the tutorials about WordPress REST API, how to create a post, how to add a featured image and so on. But did you know that WooCommerce has its own API which is build on the base of a regular WordPress REST API but allows to do much more things and also simplifies the work with the other ones.

In this tutorial we are going to dive into actual examples of working with WooCommerce API, I will show examples of creating and updating products, and also we will try to search them by SKU and delete them.

One more thing – I am a big fan of using WordPress HTTP functions like wp_remote_get() and wp_remote_request(), so we are going to use them below. So no additional steps are needed like installation of a PHP library or anything else.

Consumer Key and Consumer Secret

To work with WooCommerce API you should have a consumer key and a consumer secret, you can generate both of them in WooCommerce settings of a store you’re going to interact with.

In WooCommerce settings please go to Advanced tab and then to REST API tab.

how to get WooCommerce REST API keys

Just click that big button.

configuring the rest api credentials in WooCommerce

The settings above are pretty much straightforward, so Description is the description of these specific pair of keys. User – is a store user under whose permissions all the changes are going to be made and finally Permissions – if you set just “Read” there, so you will be able only to get the data using these API credentials but not allowed to update anything on the store. So let’s set “Read/Write”.

Finally we got our keys:

what are consumer key and consumer secret in WooCommerce

One more thing to notion, please make sure that your REST API isn’t disabled on the WordPress level, for example it is possible to do with this code. Also there are plugins for that.

So for all the example below we assume that we have two variables declared – $key and $secret.

Create Product with WooCommerce API

Guys, a small disclaimer – it is just examples, not the complete API docs. So, obviously there are a lot of more arguments available, which you can find in the official docs.

Creating products is very similar to creating posts with WordPress REST API.

$response = wp_remote_post( 
	'https://your-website/wp-json/wc/v3/products', 
	array(
	 	'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$key:$secret" )
		),
		'body' => array(
			'name' => 'My test product', // product title
			'status' => 'draft', // product status, default: publish
			'categories' => array( // product categories
				array(
					'id' => 5 // each category in a separate array
				),
				array(
					'id' => 10
				)
			),
			'regular_price' => '9.99' // product price
		)
	)
);

if( 'Created' === wp_remote_retrieve_response_message( $response ) ) {
	$body = json_decode( wp_remote_retrieve_body( $response ) );
	echo 'The product ' . $body->name . ' has been created';
}

You can always print_r( $body ) or print_r( $response ) to view what is returned.

If you’re going to create products with attributes using WooCommerce API, I could recommend you my another tutorial with the appropriate examples there.

Update Product with WooCommerce API

In this piece of code you have to replace {PRODUCT ID} with the actual product ID you would like to update.

$response = wp_remote_request( 
	'https://your-website/wp-json/wc/v3/products/{PRODUCT ID}', 
	array(
		'method' => 'PUT',
 		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$key:$secret" )
		),
		'body' => array(
    	'regular_price' => '100.30', // just update the product price
			// but we can update several parameters at the same time
		)
	)
);

if( 'OK' === wp_remote_retrieve_response_message( $response ) ) {
	$body = json_decode( wp_remote_retrieve_body( $response ) );
	echo 'The product ' . $body->name . ' has been updated';
}

Search Products

Another cool thing is that WooCommerce REST API allows you to search products let’s say by their SKU or product categories.

Let me show you an example how you can get products or even product variation by SKU.

$response = wp_remote_get(
	add_query_arg( 
		array( '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( $response ) ) {
	// now we have an array of products found
	$products = json_decode( wp_remote_retrieve_body( $response ) );
	// but probably it is the only product because we are searching by SKU
	$product = reset( $products );
	if( 'variation' === $product->type ) {
		echo 'Variation found with ID: ' . $product->id;
	} else {
		echo 'Product found with ID: ' . $product->id;
	}
}

Delete a Product

Do not forget to replace {PRODUCT ID} with the product ID. ?force=true at the end of the endpoint url means to remove a product without moving to trash.

$response = wp_remote_request( 
	'https://your-website/wp-json/wc/v3/products/{PRODUCT ID}', // ?force=true
	array( 
		'method' => 'DELETE',
 		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$key:$secret" )
		)
	)
);

if( 'OK' === wp_remote_retrieve_response_message( $response ) ) {
	$body = json_decode( wp_remote_retrieve_body( $response ) );
	echo 'The product ' . $body->name . ' has been removed';
}
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