1
1
# wc-api-custom-meta
2
2
WordPress/WooCommerce plugin to support custom meta fields through the product API
3
3
4
- A very simple plugin that allows you to create, update or remove custom meta fields when
5
- managing prducts through the WooCommerce API.
4
+ A very simple plugin that allows you to view, create, update or remove custom meta fields when
5
+ managing products through the WooCommerce API v2 .
6
6
7
7
It is necessary to install this plugin on the WC site providing the API.
8
+
8
9
I have raised a ticket asking whether this can ever be a core feature of WooCommerce,
9
10
but the ticket has been rejected:
10
11
11
12
https://github.com/woothemes/woocommerce/issues/7593
12
13
13
14
It may come back, but this plugin fills a gap in the meantime.
14
15
15
- To use it, add elements to the product array passed to the ` products ` entrypoint. Each element is
16
- an array, with examples that follow:
16
+ ## Modifying Product Meta
17
+
18
+ To use it, add elements to the product array passed to the ` products ` POST entrypoint.
19
+ Each element is an array, with examples below. Both ` custom_meta ` and ` remove_custom_meta `
20
+ elements are optional.
17
21
18
22
~~~ php
19
23
$product = [
@@ -22,6 +26,7 @@ $product = [
22
26
...
23
27
'custom_meta' => [
24
28
'my_custom_field_name' => 'my custom value',
29
+ 'my_other_custom_field_name' => 'my other custom value',
25
30
],
26
31
'remove_custom_meta' => [
27
32
'remove_all_instances_of_this_field_name',
@@ -32,9 +37,9 @@ $product = [
32
37
~~~
33
38
34
39
That's it. Make sure those elements are in your REST API request, and this plugin is installed at the other end,
35
- and you can set any meta fields you like.
40
+ and you can set any meta fields you like, except for the protected fields (see below) .
36
41
37
- I have not tested this with anything other than strings, so be wary that the behaviour storing other data structures
42
+ I have not tested this with anything other than strings, so be aware that the behaviour storing other data structures
38
43
in metafields are * undefined* at present.
39
44
40
45
## Returning Product Meta
@@ -43,17 +48,17 @@ When retrieving a product, the custom product meta fields will be put into the "
43
48
retrieving a product through the API. This only works for capability 'manage_woocommerce' to help prevent
44
49
leakage of secure data.
45
50
46
- The field will look something like this:
47
-
48
- "meta": [
51
+ The returned ` meta ` field will look something like this in structure:
49
52
50
- {
51
- "test custom field": "Hi There!",
52
- "pv_commission_rate": "",
53
- "ISBN": "1234567890-1"
54
- }
55
- ]
53
+ ~~~ json
54
+ "meta" :
55
+ {
56
+ "test custom field" : " Hi There!" ,
57
+ "pv_commission_rate" : " 20%" ,
58
+ "ISBN" : " 978-1910223260"
59
+ }
60
+ ~~~
56
61
57
- Both visibla and hidden ("_ " prefixed ) fields will be included in the list. All raw WooCommerce fields that
62
+ Both visible and hidden (with "_ " prefixes ) fields will be included in the list. All raw WooCommerce fields that
58
63
are already present in some form in the product data will be filtered out, leaving only fields added by
59
64
third-party plugins or manually by the shop manager.
0 commit comments