Create or Update WooCommerce Orders via REST API

Yes, I know, I already have plenty of tutorials on this topic on my blog, for example I’ve showed you how to do the same for products, customers and so on. And now it is time to do it for WooCommerce orders.

By the way, if you’re looking for a way how you to delete orders using REST API, check this out.

Before we begin, let’s decide how we’re going to send REST API requests in WooCommerce, because there are different ways to do it actually.

Let’s say we are going to send them from another WordPress website, then we have the following possible ways:

I am going to use the first way in the examples below because it doesn’t require me to install anything extra. But you can use whatever way you want.

Create an Order Using REST API

You can create a WooCommerce order just by sending a POST request to this endpoint /wp-json/wc/v3/orders. By the way there are no required parameters you need to pass in order to create an order, so something like this will be enough:

$response = wp_remote_post(
	"{$url}/wp-json/wc/v3/orders",
	array(
	 	'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$username:$pwd" )
		),
		'body' => array()
	)
);
if( 'Created' === wp_remote_retrieve_response_message( $response ) ) {
	echo 'Your order has been created';
}

Of course do not forget to replace $url with the actual WooCommerce store where you about to create orders using the REST API, $username and $pwd please replace with a username and an application password.

And that’s what we get if we run the code above:

a WooCommerce order created with REST API without parameters

Of course it is better to pass some parameters to an order, at least a customer name, maybe order total or a payment method, you can find the full list of parameters in an official WooCommerce REST API documentation, but for now – just check the example below:

$response = wp_remote_post(
	"{$url}/wp-json/wc/v3/orders",
	array(
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$username:$pwd" )
		),
		'body' => array(
			'status' => 'processing',
			'customer_id' => 1,
			'billing' => array(
				'first_name' => 'Misha',
				'last_name' => 'Rudrastyh',
				'company' => 'Misha Rudrastyh Digital',
				'address_1' => 'Boulevard Point',
				'address_2' => '2301',
				'city' => 'Dubai',
				'state' => 'Dubai',
				'postcode' => '0000',
				'country' => 'AE', // in in ISO 3166-1 alpha-2 format
				'email' => 'no-reply@rudrastyh.com',
				'phone' => '+971 000 00 00',
			),
			'shipping' => array(), // the same as for 'billing'
			'payment_method' => 'cod',
			'payment_method_title' => 'By cash', // you can set a custom title
			'transaction_id' => 'tx1234567890',
			'meta_data' => array(
				array(
					'key' => 'my_custom_key',
					'value' => 'the value of this key',
				),
			),
			// products added to an order
			'line_items' => array(
				// when a product exists on this store
				array(
					'product_id' => 123, // or 'variation_id' => 123,
					'quantity' => 2,
				),
				// when a product doesn't exist
				array(
					'name' => 'Surf board',
					'quantity' => 2,
					'subtotal' => 50,
					'total' => 100,
					'product_id' => 9999, // you can use non-existing ID
				),
			),
			'shipping_lines' => array(
				array(
					'method_title' => 'My shipping method',
					'method_id' => 'flat_rate',
					'total' => 5,
				),
			),
			'fee_lines' => array(
				array(
					'name' => 'Some special handling fee',
					'total' => 500,
				),
			),
			'coupon_lines' => array(
				array(
					'code' => 'blckfrd',
				),
			),
			// if you set this parameter to true it will reduce stock items quantity and set the order status to processing
			//'set_paid' => true,
		)
	)
);

Update an Existing Order Using REST API

Updating an order is as easy as to send a PUT REST API request to the /wp-json/wc/v3/orders/{ORDER ID} endpoint.

Change an order status

Let’s begin with something super-simple. For example we can change an order status.

$response = wp_remote_request(
	"{$url}/wp-json/wc/v3/orders/{$order_id}",
	array(
		'method' => 'PUT',
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$username:$pwd" )
		),
		'body' => array(
			'status' => 'completed', // pending, processing, on-hold, cancelled, refunded, failed
		)
	)
);
if( 'OK' === wp_remote_retrieve_response_message( $response ) ) {
	echo 'Order status has been changed.';
}

Update a custom field of an order

Updating custom fields of an order is also not a very big deal:

$response = wp_remote_request(
	"{$url}/wp-json/wc/v3/orders/{$order_id}",
	array(
		'method' => 'PUT',
		'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$username:$pwd" )
		),
		'body' => array(
			'meta_data' => array(
				array(
					'key' => 'my_custom_key',
					'value' => 'a new custom field value',
				),
			),
		)
	)
);

The new value of a custom field should be reflected on edit order page, in case you don’t have your field added into the order meta box, it will be displayed just like that:

Update WooCommerce order custom field with REST API

Add new line items

You can easily add an order item to an order without removing the existing ones. You just need to pass them into line_items, shipping_lines or fee_lines array.

For example we would like to add a product variation to an order as an order item:

$response = wp_remote_request(
	"{$url}/wp-json/wc/v3/orders/{$order_id}",
	array(
		'method' => 'PUT',
	 	'headers' => array(
			'Authorization' => 'Basic ' . base64_encode( "$username:$pwd" )
		),
		'body' => array(
			'line_items' => array(
				array(
					'variation_id' => 12345,
				),
			),
		)
	)
);

That’s great but if we need to remove existing order items from an order as well? The good news – all you need to do is to pass a quantity of an order item equal to 0, the bad news – you need to know an order item ID, example:

'line_items' => array(
	array(
		'id' => 1234, // yes, we need to provide an order item ID
		'quantity' => 0,
	),
),
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 X