14

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!

4
  • What are you specifically trying to do that isn't covered by their documentation? That would make a more specific question. Commented Apr 2, 2016 at 17:57
  • Custom fields that usually added by other plugins such as order types for example. Or if I want to create a custom field for an order using an API call. Commented Apr 2, 2016 at 20:01
  • 2
    You'll need to dig the code in plugins/woocommerce/includes/api/*.php files, find the correct action or filter hook and use it. For eg: when WooCommerce creates an order via the API, after creating the order it offers the following hook do_action( 'woocommerce_api_create_order', $order->id, $data, $this ); in that hook you have access to the $data that was sent , you can extract out your custom field values and process it. Commented Apr 4, 2016 at 10:47
  • this one is a bit more detailed : stackoverflow Commented Aug 18, 2020 at 18:01

3 Answers 3

18

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.

Sign up to request clarification or add additional context in comments.

16 Comments

It absolutely does. I use it extensively in my app. It depends on which version of the API you are using. The new v1 API is based on WP API. So that means there is no parent dictionary.
@Daz hello! Yes, I had to override the Order-class with a new class. I did it like this, github.com/XiaoFaye/WooCommerce.NET/issues/73. What kind of data do you need to access and which payment gateway are you using?
@Magnetize Thanks for replying, that's interesting that the API does a bit more than what is documented. I want to pull transaction IDs for a SagePay payment, at the moment I presume these are in the meta but haven't confirmed this yet.
@Daz no problem. The creator of WooCommerceNET replied in the thread above that he will be adding "meta_data" in the next update. Hmm when I was looking through the code in /plugins/woocoommerce/includes/api/class-wc-rest-orders-controller.php I noticed (at row 1055) that the fields must not be protected (i.e. cannot have names with leading _ ) in order to be updated by the API, and transaction ID's are named with a leading _ I am afraid. I have not tried it though, but I think you might be up for quite a challenge. :/
@Magnetize Thanks for the heads up - challenge = situation normal :-)
|
1

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.

1 Comment

To understand the database structure, see the WooCommerce schema and the WordPress schema. WordPress metadata allows for custom key/value pairs.
0

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"
      }]
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.