Reputation: 1687
Is it possible to access custom fields for orders, products, customers via WooCommerce REST API? If not natively, then what plugins or workarounds or hacks are out there that work? Thanks!
Upvotes: 14
Views: 25411
Reputation: 2809
SIMPLE SOLUTION THAT WORKED FOR ME (using REST API REQUEST):
URL: https:///wp-json/wc/v3/orders/1234
METHOD: PUT
BODY:
{
"status": "completed",
"meta_data": [{
"key": "is_web_server_handled",
"value": "1"
}]
}
Upvotes: 0
Reputation: 1687
Answering my own question:
It is possible using the following: (using v3 legacy API)
To send custom fields back to the server: (For Orders)
{
"order_meta": {
"key": "value"
}
}
To retrieve custom fields from server use this filter with your end point:
http://www.example.com/wc-api/v3/orders?filter[meta]=true
This works for Products as well.
Upvotes: 18
Reputation: 14913
As mentioned in the comment after WooCommerce creates an order via the API it will fire woocommerce_api_create_order
hook, you can make use of it.
Add the following code to your theme's functions.php file
add_action( 'woocommerce_api_create_order', 'my_woocommerce_api_create_order', 10, 2);
function my_woocommerce_api_create_order( $order_id, $data ) {
// $data contains the data was posted, add code to extract the required
// fields and process it as required
}
Similarly look at the code in plugins/woocommerce/includes/api/*.php
files, find the suitable action or filter hook for the end point and use it.
Upvotes: 1