11

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!

godel9
  • 7,179
  • 1
  • 30
  • 51
Amjad
  • 1,566
  • 5
  • 18
  • 40
  • What are you specifically trying to do that isn't covered by their [documentation](https://woothemes.github.io/woocommerce-rest-api-docs/)? That would make a more specific question. – helgatheviking Apr 02 '16 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. – Amjad Apr 02 '16 at 20:01
  • 1
    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. – Anand Shah Apr 04 '16 at 10:47
  • this one is a bit more detailed : [stackoverflow](https://stackoverflow.com/questions/28952970/woocommerce-api-update-order-meta-fields) – yoni Aug 18 '20 at 18:01

2 Answers2

15

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.

Amjad
  • 1,566
  • 5
  • 18
  • 40
  • Can you point to the docs where this is documented. I am fighting the same issue and am pretty much at a dead end. – Ivan Crojach Karačić May 18 '16 at 12:43
  • Its not documented on WooCommerce API pages. I got the answer from their github issues. This field is a hidden meta field. If you have a specific question you can ask here. – Amjad May 18 '16 at 23:00
  • We have a Custom Taxonomy `Status` assigned to each product which I would like to get with the Rest API but I don't know how to construct the query string in order to get the data back – Ivan Crojach Karačić May 20 '16 at 06:30
  • 1
    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. – Amjad Jul 15 '16 at 21:55
  • @HelloWorld hi there. I am trying to update order meta through a C# wrapper for Woocommerce, and I was just wondering what you mean by "parent dictionary". The API does not find my data and the wrapper does not take meta_data into account since it is missing from the docs. – Magnetize Jan 04 '17 at 08:22
  • @Magnetize did you have to hack the wrapper to get this working? I'm trying to access payment gateway data for an order via WooCommerceNET.WooCommerce. – Daz Jan 06 '17 at 10:11
  • 1
    @Daz hello! Yes, I had to override the Order-class with a new class. I did it like this, https://github.com/XiaoFaye/WooCommerce.NET/issues/73. What kind of data do you need to access and which payment gateway are you using? – Magnetize Jan 07 '17 at 12:01
  • 1
    @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 Jan 08 '17 at 22:04
  • 1
    @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 Jan 09 '17 at 07:38
  • 1
    @Magnetize Thanks for the heads up - challenge = situation normal :-) – Daz Jan 09 '17 at 10:22
  • Does this answer work for products as well as orders? – Warlike Chimpanzee Jun 17 '17 at 17:20
  • @WarlikeChimpanzee yes it does. – Amjad Feb 28 '19 at 02:58
  • It is also possible with the latest version of WooCommerce API and Advanced Custom Fields. – Amjad Mar 12 '19 at 18:53
  • @Amjad hi. Can you please add an example on how can this be done with the latest version of WooCommerce API and Advanced Custom Fields? I tried `filter[meta]=true`, but it did not work for me. – Jehanzeb.Malik Aug 25 '19 at 22:18
  • Nevermind. I found it. The Advanced Custom Fields data comes under `meta_data`. The issue was that I was setting default values for the custom fields when creating product but they were not being saved with product. Only when I saved some value in those fields and saved the product was I able to view them. – Jehanzeb.Malik Aug 26 '19 at 08:38
  • Yes you need to save the product first in order for the custom fields to appear in meta data. – Amjad Aug 26 '19 at 23:22
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.

Anand Shah
  • 13,453
  • 15
  • 65
  • 105
  • 1
    To understand the database structure, see the [WooCommerce schema](https://github.com/woocommerce/woocommerce/wiki/Database-Description) and the [WordPress schema](https://codex.wordpress.org/images/2/25/WP4.4.2-ERD.png). WordPress metadata allows for custom key/value pairs. – Warlike Chimpanzee Jun 18 '17 at 14:44