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.
The custom fields data are saved in the wp_postmeta table and the value can be retrieved using the below code. get_post_meta($order_id, 'field_name', true); To update the value, you can use the below code. update_post_meta($order_id, 'field_name', $field_value);
The WooCommerce REST API is an interface that you can use to access your WooCommerce store from outside WordPress. It was designed to make it easy for WooCommerce stores on WordPress to interact with other websites and applications over the Internet.
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.
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.
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"
}
]
}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With