Reputation: 1806
Does the WooCommerce Rest API offer any way to update meta fields?
They have an Extension which adds the ability to add tracking numbers to an order when it's shipped using meta fields, but don't have any documentation on how or if it's possible to update the order's meta fields with this information via their REST API.
Upvotes: 11
Views: 22359
Reputation: 1806
2020 Update:
The latest versions of the WooCommerce API allow updating meta fields that start with an underscore. Yay! You can do so in bulk as big_water mentioned below: https://stackoverflow.com/a/44661445/137695
You can also update meta fields for a single order using the new V2 or V3 WooCommerce API Order Update PUT endpoint: https://woocommerce.github.io/woocommerce-rest-api-docs/#update-an-order
"meta_data": [
{
"key": "_some_field",
"value": "some value"
},
{
"key": "_some_other_field",
"value": "some other value"
}
]
OLD ANSWER BELOW - Regarding the old WooCommerce API endpoints only:
It's poorly (read not at all) documented how to do this. Here's how to do it for normal meta fields:
curl -X PUT "https://somesite.com/wc-api/v2/orders/124?filter[meta]=true" -u ck_yourconsumerkey:cs_yourconsumersecret -H "Content-Type: application/json" -d
'{
"order": {
"order_meta": {"meta_key":"meta_value"}
}
}'
Meta fields that start with an underscore are protected and cannot be updated via the API. However, according to this GitHub issue the next version of WooCommerce should support updating protected meta fields as long as they aren't WooCommerce internal values.
There are Meta fields for other parts of the order like customer and item, but it would take me a while to dig up info on those again and this question was specifically regarding Order level meta fields.
Edit: WooCommerce has changed their minds and closed the GitHub case saying they will not allow access to these fields. This unfortunately makes WooCommerce one of the only shopping carts where it is impossible to add tracking information to an order via the API without a terrible hackish workaround.
Upvotes: 15
Reputation: 151
You can use a HTTP POST request to the "/wp-json/wc/v3/orders" endpoint with the extra "meta_data" field as shown in the following curl command. This is tested using the "REST API for Woocommerce version 3.0"
curl --request POST \
--url https://yourserver.com/wp-json/wc/v3/orders \
--header 'authorization: Basic <credentials>' \
--header 'content-type: application/json' \
--data '{
"line_items": [
{
"product_id": 425,
"quantity": 1
}
],
"meta_data":[
{
"key": "custom key",
"value": "custom value"
}
]
}'
Upvotes: 2
Reputation: 3204
Yes it is possible. Using the REST API for Woocommerce version 3.0, I added custom order tracking and carrier fields using the following json on the endpoint described in their latest documentation here.
{
"create":[],
"update": [
{
"id": 77248,
"status": "shipping",
"meta_data":[
{
"key": "package_carrier",
"value": "USPS First Class"
},
{
"key": "tracking_number",
"value": "12354LKJSDF"
}
]
}
]
}
They then show on the order edit screen:
This is obviously using the batch update for order, however, this should still work for the single orders endpoint as well.
Since I'm not including an "id" field, it creates a new one. I believe to update the fields, you need to supply the "id" field.
Upvotes: 7