WooCommerce REST API – Create, Update or Remove Products

This is the basic tutorial – you will learn how to use WooCommerce REST API. In the next more advanced tutorials we will talk about product attributes and variations.

/9 comments

Where to Get REST API Keys – 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 under the API tab.

You can generate your Consumer key and Consumer secret in WooCommerce settings under the API tab.

I think I shouldn’t mention that you have to make sure that REST API checkbox must be enabled. 🙃 If you want to completely disable REST API, look at this code example.

In this tutorial we will also need Read/Write permissions.

Create a Product – it is so simple!

Creating products is very similar to creating posts with WordPress REST API. Only the parameters and the endpoint differ.

Pair KEY:SECRET we obtain in the first step.

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v2/products', array(
 	'headers' => array(
		'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
	),
	'body' => array(
		'name' => 'My test2', // product title
		'status' => 'draft', // product status, default: publish
		'categories' => array(
			array( 
				'id' => 5 // each category in a separate array
			),
			array(
				'id' => 10
			)
		),
		'regular_price' => '9.99' // product price
		// more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
	)
) );
 
$body = json_decode( $api_response['body'] );
//print_r( $body );
 
if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
	echo 'The product ' . $body->name . ' has been created';
}

You can always print_r( $body ) or print_r( $api_response ) to view all parameters returned.

Update Product Price with WooCommerce REST API

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

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v2/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
		// more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
	)
) );
 
$body = json_decode( $api_response['body'] );
//print_r( $body );
 
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
	echo 'The product ' . $body->name . ' has been updated';
}

Remove 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.

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v2/products/{PRODUCT ID}', array(  // ?force=true
	'method'    => 'DELETE',
 	'headers' => array(
		'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
	)
) );
 
$body = json_decode( $api_response['body'] );
//print_r( $body );
 
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
	echo 'The product ' . $body->name . ' has been removed';
}

If you have any question, feel free to leave it in comments below.

More REST API

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 9

Leave a comment

php js HTML CSS Code