-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathclass-wc-api-client-resource-products.php
More file actions
243 lines (197 loc) · 4.94 KB
/
class-wc-api-client-resource-products.php
File metadata and controls
243 lines (197 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* WC API Client Products resource class
*
* @since 2.0
*/
class WC_API_Client_Resource_Products extends WC_API_Client_Resource {
/**
* Setup the resource
*
* @since 2.0
* @param WC_API_Client $client class instance
*/
public function __construct( $client ) {
parent::__construct( 'products', 'product', $client );
}
/**
* Get products
*
* GET /products
* GET /products/#{id}
*
* @since 2.0
* @param null|int $id product ID or null to get all products
* @param array $args acceptable products endpoint args, like `filter[]`
* @return array|object products!
*/
public function get( $id = null, $args = array() ) {
$this->set_request_args( array(
'method' => 'GET',
'path' => $id,
'params' => $args,
) );
return $this->do_request();
}
/**
* Get product by SKU
*
* GET /products/?filter[{sku}]
*
* Note this will throw an exception if no products are found (404 not found)
*
* @since 3.0
* @param string $sku product SKU
* @param array $args acceptable product SKU lookup endpoint args, currently only `fields`
* @return array|object product!
*/
public function get_by_sku( $sku, $args = array() ) {
$this->set_request_args( array(
'method' => 'GET',
'params' => array_merge( array( "filter[sku]" => $sku ), $args )
) );
return $this->do_request();
}
/**
* Create a product
*
* POST /products
*
* @since 2.0
* @param array $data valid product data
* @return array|object your newly-created product
*/
public function create( $data ) {
$this->set_request_args( array(
'method' => 'POST',
'body' => $data,
) );
return $this->do_request();
}
/**
* Update a product
*
* PUT /product/#{id}
*
* @since 2.0
* @param int $id product ID
* @param array $data product data to update
* @return array|object your newly-updated product
*/
public function update( $id, $data ) {
$this->set_request_args( array(
'method' => 'PUT',
'path' => $id,
'body' => $data,
) );
return $this->do_request();
}
/**
* Delete a product
*
* DELETE /products/#{id}
*
* @since 2.0
* @param int $id product ID
* @param bool $force true to permanently delete the product, false to trash it
* @return array|object response
*/
public function delete( $id, $force = false ) {
$this->set_request_args( array(
'method' => 'DELETE',
'path' => $id,
'params' => array( 'force' => $force ),
) );
return $this->do_request();
}
/**
* Get a count of products
*
* GET /products/count
*
* @since 2.0
* @param array $args acceptable product endpoint args, like `type` or `filter[]`
* @return array|object the count
*/
public function get_count( $args = array() ) {
$this->set_request_args( array(
'method' => 'GET',
'path' => 'count',
'params' => $args,
) );
return $this->do_request();
}
/**
* Get product reviews
*
* GET /products/#{product_id}/reviews
*
* @since 2.0
* @param int $id product ID
* @param array $args acceptable product reviews endpoint args, currently only `fields`
* @return array|object product reviews!
*/
public function get_reviews( $id, $args = array() ) {
$this->set_request_args( array(
'method' => 'GET',
'path' => array( $id, 'reviews' ),
'params' => $args,
) );
return $this->do_request();
}
/**
* Get a list of product categories or a single product category
*
* GET /products/categories
* GET /products/categories/{#id}
*
* @since 2.0
* @param int $id category ID or null to get all product categories
* @param array $args acceptable product categories endpoint args, currently only `fields`
* @return array|object product categories!
*/
public function get_categories( $id = null, $args = array() ) {
$this->set_request_args( array(
'method' => 'GET',
'path' => array( 'categories', $id ),
'params' => $args,
) );
return $this->do_request();
}
/**
* Retrieve all product orders.
*
* GET /products/{#id}/orders
*
* @since 3.0
* @param int $id category ID or null to get all product categories
* @param array $args acceptable product categories endpoint args, currently only `fields`
* @return array|object product categories!
*/
public function get_product_orders( $id = null, $args = array() ) {
$this->set_request_args( array(
'method' => 'GET',
'path' => array( $id, 'orders' ),
'params' => $args,
) );
return $this->do_request();
}
/** Convenience methods - these do not map directly to an endpoint ********/
/**
* Update the stock quantity for a product
*
* PUT /products/#{id} with stock quantity
*
* @param int $id order ID
* @param int|float $quantity new stock quantity
* @return array|object newly-updated product
*/
public function update_stock( $id, $quantity ) {
$this->set_request_args( array(
'method' => 'PUT',
'path' => $id,
'body' => array( 'stock_quantity' => $quantity ),
) );
return $this->do_request();
}
}