|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* REST API variations controller |
|
4
|
|
|
* |
|
5
|
|
|
* Handles requests to the /products/<product_id>/variations endpoints. |
|
6
|
|
|
* |
|
7
|
|
|
* @package WooCommerce/RestApi |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace WooCommerce\RestApi\Controllers\Version4; |
|
11
|
|
|
|
|
12
|
|
|
defined( 'ABSPATH' ) || exit; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* REST API variations controller class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class ProductVariations extends Products { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Route base. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $rest_base = 'products/(?P<product_id>[\d]+)/variations'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Post type. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $post_type = 'product_variation'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Initialize product actions (parent). |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct() { |
|
37
|
|
|
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'add_product_id' ), 9, 2 ); |
|
38
|
|
|
parent::__construct(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Register the routes for products. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function register_routes() { |
|
45
|
|
|
register_rest_route( |
|
46
|
|
|
$this->namespace, |
|
47
|
|
|
'/' . $this->rest_base, |
|
48
|
|
|
array( |
|
49
|
|
|
'args' => array( |
|
50
|
|
|
'product_id' => array( |
|
51
|
|
|
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ), |
|
52
|
|
|
'type' => 'integer', |
|
53
|
|
|
), |
|
54
|
|
|
), |
|
55
|
|
|
array( |
|
56
|
|
|
'methods' => \WP_REST_Server::READABLE, |
|
57
|
|
|
'callback' => array( $this, 'get_items' ), |
|
58
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
59
|
|
|
'args' => $this->get_collection_params(), |
|
60
|
|
|
), |
|
61
|
|
|
array( |
|
62
|
|
|
'methods' => \WP_REST_Server::CREATABLE, |
|
63
|
|
|
'callback' => array( $this, 'create_item' ), |
|
64
|
|
|
'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
65
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
|
66
|
|
|
), |
|
67
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
68
|
|
|
), |
|
69
|
|
|
true |
|
70
|
|
|
); |
|
71
|
|
|
register_rest_route( |
|
72
|
|
|
$this->namespace, |
|
73
|
|
|
'/' . $this->rest_base . '/(?P<id>[\d]+)', |
|
74
|
|
|
array( |
|
75
|
|
|
'args' => array( |
|
76
|
|
|
'product_id' => array( |
|
77
|
|
|
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ), |
|
78
|
|
|
'type' => 'integer', |
|
79
|
|
|
), |
|
80
|
|
|
'id' => array( |
|
81
|
|
|
'description' => __( 'Unique identifier for the variation.', 'woocommerce' ), |
|
82
|
|
|
'type' => 'integer', |
|
83
|
|
|
), |
|
84
|
|
|
), |
|
85
|
|
|
array( |
|
86
|
|
|
'methods' => \WP_REST_Server::READABLE, |
|
87
|
|
|
'callback' => array( $this, 'get_item' ), |
|
88
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
89
|
|
|
'args' => array( |
|
90
|
|
|
'context' => $this->get_context_param( |
|
91
|
|
|
array( |
|
92
|
|
|
'default' => 'view', |
|
93
|
|
|
) |
|
94
|
|
|
), |
|
95
|
|
|
), |
|
96
|
|
|
), |
|
97
|
|
|
array( |
|
98
|
|
|
'methods' => \WP_REST_Server::EDITABLE, |
|
99
|
|
|
'callback' => array( $this, 'update_item' ), |
|
100
|
|
|
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
101
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
|
102
|
|
|
), |
|
103
|
|
|
array( |
|
104
|
|
|
'methods' => \WP_REST_Server::DELETABLE, |
|
105
|
|
|
'callback' => array( $this, 'delete_item' ), |
|
106
|
|
|
'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
107
|
|
|
'args' => array( |
|
108
|
|
|
'force' => array( |
|
109
|
|
|
'default' => false, |
|
110
|
|
|
'type' => 'boolean', |
|
111
|
|
|
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
|
112
|
|
|
), |
|
113
|
|
|
), |
|
114
|
|
|
), |
|
115
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
116
|
|
|
), |
|
117
|
|
|
true |
|
118
|
|
|
); |
|
119
|
|
|
register_rest_route( |
|
120
|
|
|
$this->namespace, |
|
121
|
|
|
'/' . $this->rest_base . '/batch', |
|
122
|
|
|
array( |
|
123
|
|
|
'args' => array( |
|
124
|
|
|
'product_id' => array( |
|
125
|
|
|
'description' => __( 'Unique identifier for the variable product.', 'woocommerce' ), |
|
126
|
|
|
'type' => 'integer', |
|
127
|
|
|
), |
|
128
|
|
|
), |
|
129
|
|
|
array( |
|
130
|
|
|
'methods' => \WP_REST_Server::EDITABLE, |
|
131
|
|
|
'callback' => array( $this, 'batch_items' ), |
|
132
|
|
|
'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
|
133
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
|
134
|
|
|
), |
|
135
|
|
|
'schema' => array( $this, 'get_public_batch_schema' ), |
|
136
|
|
|
), |
|
137
|
|
|
true |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get object. |
|
143
|
|
|
* |
|
144
|
|
|
* @since 3.0.0 |
|
145
|
|
|
* @param int $id Object ID. |
|
146
|
|
|
* @return \WC_Data|bool |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function get_object( $id ) { |
|
149
|
|
|
return wc_get_product( $id ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Check if a given request has access to update an item. |
|
154
|
|
|
* |
|
155
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
|
156
|
|
|
* @return \WP_Error|boolean |
|
157
|
|
|
*/ |
|
158
|
|
|
public function update_item_permissions_check( $request ) { |
|
159
|
|
|
$object = $this->get_object( (int) $request['id'] ); |
|
160
|
|
|
|
|
161
|
|
|
if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'edit', $object->get_id() ) ) { |
|
162
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
// Check if variation belongs to the correct parent product. |
|
166
|
|
|
if ( $object && 0 !== $object->get_parent_id() && absint( $request['product_id'] ) !== $object->get_parent_id() ) { |
|
167
|
|
|
return new \WP_Error( 'woocommerce_rest_cannot_edit', __( 'Parent product does not match current variation.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return true; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Prepare a single variation output for response. |
|
175
|
|
|
* |
|
176
|
|
|
* @param \WC_Data $object Object data. |
|
177
|
|
|
* @param \WP_REST_Request $request Request object. |
|
178
|
|
|
* @return \WP_REST_Response |
|
179
|
|
|
*/ |
|
180
|
|
|
public function prepare_object_for_response( $object, $request ) { |
|
181
|
|
|
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
182
|
|
|
$data = array( |
|
183
|
|
|
'id' => $object->get_id(), |
|
184
|
|
|
'name' => $object->get_name( $context ), |
|
|
|
|
|
|
185
|
|
|
'type' => $object->get_type(), |
|
|
|
|
|
|
186
|
|
|
'parent_id' => $object->get_parent_id( $context ), |
|
|
|
|
|
|
187
|
|
|
'date_created' => wc_rest_prepare_date_response( $object->get_date_created(), false ), |
|
|
|
|
|
|
188
|
|
|
'date_created_gmt' => wc_rest_prepare_date_response( $object->get_date_created() ), |
|
189
|
|
|
'date_modified' => wc_rest_prepare_date_response( $object->get_date_modified(), false ), |
|
|
|
|
|
|
190
|
|
|
'date_modified_gmt' => wc_rest_prepare_date_response( $object->get_date_modified() ), |
|
191
|
|
|
'description' => wc_format_content( $object->get_description() ), |
|
|
|
|
|
|
192
|
|
|
'permalink' => $object->get_permalink(), |
|
|
|
|
|
|
193
|
|
|
'sku' => $object->get_sku(), |
|
|
|
|
|
|
194
|
|
|
'price' => $object->get_price(), |
|
|
|
|
|
|
195
|
|
|
'regular_price' => $object->get_regular_price(), |
|
|
|
|
|
|
196
|
|
|
'sale_price' => $object->get_sale_price(), |
|
|
|
|
|
|
197
|
|
|
'date_on_sale_from' => wc_rest_prepare_date_response( $object->get_date_on_sale_from(), false ), |
|
|
|
|
|
|
198
|
|
|
'date_on_sale_from_gmt' => wc_rest_prepare_date_response( $object->get_date_on_sale_from() ), |
|
199
|
|
|
'date_on_sale_to' => wc_rest_prepare_date_response( $object->get_date_on_sale_to(), false ), |
|
|
|
|
|
|
200
|
|
|
'date_on_sale_to_gmt' => wc_rest_prepare_date_response( $object->get_date_on_sale_to() ), |
|
201
|
|
|
'on_sale' => $object->is_on_sale(), |
|
|
|
|
|
|
202
|
|
|
'status' => $object->get_status(), |
|
|
|
|
|
|
203
|
|
|
'purchasable' => $object->is_purchasable(), |
|
|
|
|
|
|
204
|
|
|
'virtual' => $object->is_virtual(), |
|
|
|
|
|
|
205
|
|
|
'downloadable' => $object->is_downloadable(), |
|
|
|
|
|
|
206
|
|
|
'downloads' => $this->get_downloads( $object ), |
|
207
|
|
|
'download_limit' => '' !== $object->get_download_limit() ? (int) $object->get_download_limit() : -1, |
|
|
|
|
|
|
208
|
|
|
'download_expiry' => '' !== $object->get_download_expiry() ? (int) $object->get_download_expiry() : -1, |
|
|
|
|
|
|
209
|
|
|
'tax_status' => $object->get_tax_status(), |
|
|
|
|
|
|
210
|
|
|
'tax_class' => $object->get_tax_class(), |
|
|
|
|
|
|
211
|
|
|
'manage_stock' => $object->managing_stock(), |
|
|
|
|
|
|
212
|
|
|
'stock_quantity' => $object->get_stock_quantity(), |
|
|
|
|
|
|
213
|
|
|
'stock_status' => $object->get_stock_status(), |
|
|
|
|
|
|
214
|
|
|
'backorders' => $object->get_backorders(), |
|
|
|
|
|
|
215
|
|
|
'backorders_allowed' => $object->backorders_allowed(), |
|
|
|
|
|
|
216
|
|
|
'backordered' => $object->is_on_backorder(), |
|
|
|
|
|
|
217
|
|
|
'weight' => $object->get_weight(), |
|
|
|
|
|
|
218
|
|
|
'dimensions' => array( |
|
219
|
|
|
'length' => $object->get_length(), |
|
|
|
|
|
|
220
|
|
|
'width' => $object->get_width(), |
|
|
|
|
|
|
221
|
|
|
'height' => $object->get_height(), |
|
|
|
|
|
|
222
|
|
|
), |
|
223
|
|
|
'shipping_class' => $object->get_shipping_class(), |
|
|
|
|
|
|
224
|
|
|
'shipping_class_id' => $object->get_shipping_class_id(), |
|
|
|
|
|
|
225
|
|
|
'image' => $this->get_image( $object ), |
|
226
|
|
|
'attributes' => $this->get_attributes( $object ), |
|
227
|
|
|
'menu_order' => $object->get_menu_order(), |
|
|
|
|
|
|
228
|
|
|
'meta_data' => $object->get_meta_data(), |
|
229
|
|
|
); |
|
230
|
|
|
|
|
231
|
|
|
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
232
|
|
|
$data = $this->add_additional_fields_to_object( $data, $request ); |
|
233
|
|
|
$data = $this->filter_response_by_context( $data, $context ); |
|
234
|
|
|
$response = rest_ensure_response( $data ); |
|
235
|
|
|
$response->add_links( $this->prepare_links( $object, $request ) ); |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Filter the data for a response. |
|
239
|
|
|
* |
|
240
|
|
|
* The dynamic portion of the hook name, $this->post_type, |
|
241
|
|
|
* refers to object type being prepared for the response. |
|
242
|
|
|
* |
|
243
|
|
|
* @param \WP_REST_Response $response The response object. |
|
244
|
|
|
* @param \WC_Data $object Object data. |
|
245
|
|
|
* @param \WP_REST_Request $request Request object. |
|
246
|
|
|
*/ |
|
247
|
|
|
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request ); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get the image for a product variation. |
|
252
|
|
|
* |
|
253
|
|
|
* @param \WC_Product_Variation $variation Variation data. |
|
254
|
|
|
* @return array |
|
255
|
|
|
*/ |
|
256
|
|
|
protected function get_image( $variation ) { |
|
257
|
|
|
if ( ! $variation->get_image_id() ) { |
|
258
|
|
|
return; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$attachment_id = $variation->get_image_id(); |
|
262
|
|
|
$attachment_post = get_post( $attachment_id ); |
|
|
|
|
|
|
263
|
|
|
if ( is_null( $attachment_post ) ) { |
|
264
|
|
|
return; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' ); |
|
|
|
|
|
|
268
|
|
|
if ( ! is_array( $attachment ) ) { |
|
269
|
|
|
return; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
if ( ! isset( $image ) ) { |
|
273
|
|
|
return array( |
|
274
|
|
|
'id' => (int) $attachment_id, |
|
275
|
|
|
'date_created' => wc_rest_prepare_date_response( $attachment_post->post_date, false ), |
|
276
|
|
|
'date_created_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ), |
|
277
|
|
|
'date_modified' => wc_rest_prepare_date_response( $attachment_post->post_modified, false ), |
|
278
|
|
|
'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ), |
|
279
|
|
|
'src' => current( $attachment ), |
|
280
|
|
|
'name' => get_the_title( $attachment_id ), |
|
|
|
|
|
|
281
|
|
|
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ), |
|
|
|
|
|
|
282
|
|
|
); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Set variation image. |
|
288
|
|
|
* |
|
289
|
|
|
* @throws \WC_REST_Exception REST API exceptions. |
|
290
|
|
|
* |
|
291
|
|
|
* @param \WC_Product_Variation $variation Variation instance. |
|
292
|
|
|
* @param array $image Image data. |
|
293
|
|
|
* @return \WC_Product_Variation |
|
294
|
|
|
*/ |
|
295
|
|
|
protected function set_variation_image( $variation, $image ) { |
|
296
|
|
|
$attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; |
|
297
|
|
|
|
|
298
|
|
|
if ( 0 === $attachment_id && isset( $image['src'] ) ) { |
|
299
|
|
|
$upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) ); |
|
300
|
|
|
|
|
301
|
|
|
if ( is_wp_error( $upload ) ) { |
|
302
|
|
|
if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $variation->get_id(), array( $image ) ) ) { |
|
303
|
|
|
throw new \WC_REST_Exception( 'woocommerce_variation_image_upload_error', $upload->get_error_message(), 400 ); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $variation->get_id() ); |
|
|
|
|
|
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
|
311
|
|
|
/* translators: %s: attachment ID */ |
|
312
|
|
|
throw new \WC_REST_Exception( 'woocommerce_variation_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 ); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$variation->set_image_id( $attachment_id ); |
|
316
|
|
|
|
|
317
|
|
|
// Set the image alt if present. |
|
318
|
|
|
if ( ! empty( $image['alt'] ) ) { |
|
319
|
|
|
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) ); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
// Set the image name if present. |
|
323
|
|
|
if ( ! empty( $image['name'] ) ) { |
|
324
|
|
|
wp_update_post( |
|
325
|
|
|
array( |
|
326
|
|
|
'ID' => $attachment_id, |
|
327
|
|
|
'post_title' => $image['name'], |
|
328
|
|
|
) |
|
329
|
|
|
); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
return $variation; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Prepare objects query. |
|
337
|
|
|
* |
|
338
|
|
|
* @since 3.0.0 |
|
339
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
|
340
|
|
|
* @return array |
|
341
|
|
|
*/ |
|
342
|
|
|
protected function prepare_objects_query( $request ) { |
|
343
|
|
|
$args = parent::prepare_objects_query( $request ); |
|
344
|
|
|
|
|
345
|
|
|
// Set post_status. |
|
346
|
|
|
$args['post_status'] = $request['status']; |
|
347
|
|
|
|
|
348
|
|
|
// Set custom args to handle later during clauses. |
|
349
|
|
|
$custom_keys = array( |
|
350
|
|
|
'sku', |
|
351
|
|
|
'min_price', |
|
352
|
|
|
'max_price', |
|
353
|
|
|
'stock_status', |
|
354
|
|
|
'low_in_stock', |
|
355
|
|
|
); |
|
356
|
|
|
foreach ( $custom_keys as $key ) { |
|
357
|
|
|
if ( ! empty( $request[ $key ] ) ) { |
|
358
|
|
|
$args[ $key ] = $request[ $key ]; |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
// Filter by tax class. |
|
363
|
|
|
if ( ! empty( $request['tax_class'] ) ) { |
|
364
|
|
|
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok. |
|
365
|
|
|
$args, |
|
366
|
|
|
array( |
|
367
|
|
|
'key' => '_tax_class', |
|
368
|
|
|
'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '', |
|
369
|
|
|
) |
|
370
|
|
|
); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
// Filter by on sale products. |
|
374
|
|
|
if ( is_bool( $request['on_sale'] ) ) { |
|
375
|
|
|
$on_sale_key = $request['on_sale'] ? 'post__in' : 'post__not_in'; |
|
376
|
|
|
$on_sale_ids = wc_get_product_ids_on_sale(); |
|
377
|
|
|
|
|
378
|
|
|
// Use 0 when there's no on sale products to avoid return all products. |
|
379
|
|
|
$on_sale_ids = empty( $on_sale_ids ) ? array( 0 ) : $on_sale_ids; |
|
380
|
|
|
|
|
381
|
|
|
$args[ $on_sale_key ] += $on_sale_ids; |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
// Force the post_type argument, since it's not a user input variable. |
|
385
|
|
|
if ( ! empty( $request['sku'] ) ) { |
|
386
|
|
|
$args['post_type'] = array( 'product', 'product_variation' ); |
|
387
|
|
|
} else { |
|
388
|
|
|
$args['post_type'] = $this->post_type; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
$args['post_parent'] = $request['product_id']; |
|
392
|
|
|
|
|
393
|
|
|
if ( ! empty( $request['search'] ) ) { |
|
394
|
|
|
$args['search'] = $request['search']; |
|
395
|
|
|
unset( $args['s'] ); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
return $args; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Prepare a single variation for create or update. |
|
403
|
|
|
* |
|
404
|
|
|
* @param \WP_REST_Request $request Request object. |
|
405
|
|
|
* @param bool $creating If is creating a new object. |
|
406
|
|
|
* @return \WP_Error|\WC_Data |
|
407
|
|
|
*/ |
|
408
|
|
|
protected function prepare_object_for_database( $request, $creating = false ) { |
|
409
|
|
|
if ( isset( $request['id'] ) ) { |
|
410
|
|
|
$variation = wc_get_product( absint( $request['id'] ) ); |
|
411
|
|
|
} else { |
|
412
|
|
|
$variation = new \WC_Product_Variation(); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
$variation->set_parent_id( absint( $request['product_id'] ) ); |
|
416
|
|
|
|
|
417
|
|
|
// Status. |
|
418
|
|
|
if ( isset( $request['status'] ) ) { |
|
419
|
|
|
$variation->set_status( get_post_status_object( $request['status'] ) ? $request['status'] : 'draft' ); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
// SKU. |
|
423
|
|
|
if ( isset( $request['sku'] ) ) { |
|
424
|
|
|
$variation->set_sku( wc_clean( $request['sku'] ) ); |
|
|
|
|
|
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
// Thumbnail. |
|
428
|
|
|
if ( isset( $request['image'] ) ) { |
|
429
|
|
|
if ( is_array( $request['image'] ) ) { |
|
430
|
|
|
$variation = $this->set_variation_image( $variation, $request['image'] ); |
|
431
|
|
|
} else { |
|
432
|
|
|
$variation->set_image_id( '' ); |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
// Virtual variation. |
|
437
|
|
|
if ( isset( $request['virtual'] ) ) { |
|
438
|
|
|
$variation->set_virtual( $request['virtual'] ); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
// Downloadable variation. |
|
442
|
|
|
if ( isset( $request['downloadable'] ) ) { |
|
443
|
|
|
$variation->set_downloadable( $request['downloadable'] ); |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
// Downloads. |
|
447
|
|
|
if ( $variation->get_downloadable() ) { |
|
448
|
|
|
// Downloadable files. |
|
449
|
|
|
if ( isset( $request['downloads'] ) && is_array( $request['downloads'] ) ) { |
|
450
|
|
|
$variation = $this->save_downloadable_files( $variation, $request['downloads'] ); |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
// Download limit. |
|
454
|
|
|
if ( isset( $request['download_limit'] ) ) { |
|
455
|
|
|
$variation->set_download_limit( $request['download_limit'] ); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
// Download expiry. |
|
459
|
|
|
if ( isset( $request['download_expiry'] ) ) { |
|
460
|
|
|
$variation->set_download_expiry( $request['download_expiry'] ); |
|
461
|
|
|
} |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
// Shipping data. |
|
465
|
|
|
$variation = $this->save_product_shipping_data( $variation, $request ); |
|
466
|
|
|
|
|
467
|
|
|
// Stock handling. |
|
468
|
|
|
if ( isset( $request['manage_stock'] ) ) { |
|
469
|
|
|
$variation->set_manage_stock( $request['manage_stock'] ); |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
if ( isset( $request['stock_status'] ) ) { |
|
473
|
|
|
$variation->set_stock_status( $request['stock_status'] ); |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
if ( isset( $request['backorders'] ) ) { |
|
477
|
|
|
$variation->set_backorders( $request['backorders'] ); |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
if ( $variation->get_manage_stock() ) { |
|
481
|
|
|
if ( isset( $request['stock_quantity'] ) ) { |
|
482
|
|
|
$variation->set_stock_quantity( $request['stock_quantity'] ); |
|
483
|
|
|
} elseif ( isset( $request['inventory_delta'] ) ) { |
|
484
|
|
|
$stock_quantity = wc_stock_amount( $variation->get_stock_quantity() ); |
|
485
|
|
|
$stock_quantity += wc_stock_amount( $request['inventory_delta'] ); |
|
486
|
|
|
$variation->set_stock_quantity( $stock_quantity ); |
|
487
|
|
|
} |
|
488
|
|
|
} else { |
|
489
|
|
|
$variation->set_backorders( 'no' ); |
|
490
|
|
|
$variation->set_stock_quantity( '' ); |
|
|
|
|
|
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
// Regular Price. |
|
494
|
|
|
if ( isset( $request['regular_price'] ) ) { |
|
495
|
|
|
$variation->set_regular_price( $request['regular_price'] ); |
|
496
|
|
|
} |
|
497
|
|
|
|
|
498
|
|
|
// Sale Price. |
|
499
|
|
|
if ( isset( $request['sale_price'] ) ) { |
|
500
|
|
|
$variation->set_sale_price( $request['sale_price'] ); |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
if ( isset( $request['date_on_sale_from'] ) ) { |
|
504
|
|
|
$variation->set_date_on_sale_from( $request['date_on_sale_from'] ); |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
|
|
if ( isset( $request['date_on_sale_from_gmt'] ) ) { |
|
508
|
|
|
$variation->set_date_on_sale_from( $request['date_on_sale_from_gmt'] ? strtotime( $request['date_on_sale_from_gmt'] ) : null ); |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
if ( isset( $request['date_on_sale_to'] ) ) { |
|
512
|
|
|
$variation->set_date_on_sale_to( $request['date_on_sale_to'] ); |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
if ( isset( $request['date_on_sale_to_gmt'] ) ) { |
|
516
|
|
|
$variation->set_date_on_sale_to( $request['date_on_sale_to_gmt'] ? strtotime( $request['date_on_sale_to_gmt'] ) : null ); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
// Tax class. |
|
520
|
|
|
if ( isset( $request['tax_class'] ) ) { |
|
521
|
|
|
$variation->set_tax_class( $request['tax_class'] ); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
// Description. |
|
525
|
|
|
if ( isset( $request['description'] ) ) { |
|
526
|
|
|
$variation->set_description( wp_kses_post( $request['description'] ) ); |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
// Update taxonomies. |
|
530
|
|
|
if ( isset( $request['attributes'] ) ) { |
|
531
|
|
|
$attributes = array(); |
|
532
|
|
|
$parent = wc_get_product( $variation->get_parent_id() ); |
|
533
|
|
|
|
|
534
|
|
|
if ( ! $parent ) { |
|
|
|
|
|
|
535
|
|
|
return new \WP_Error( |
|
536
|
|
|
// Translators: %d parent ID. |
|
537
|
|
|
"woocommerce_rest_{$this->post_type}_invalid_parent", sprintf( __( 'Cannot set attributes due to invalid parent product.', 'woocommerce' ), $variation->get_parent_id() ), array( |
|
538
|
|
|
'status' => 404, |
|
539
|
|
|
) |
|
540
|
|
|
); |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
|
|
$parent_attributes = $parent->get_attributes(); |
|
544
|
|
|
|
|
545
|
|
|
foreach ( $request['attributes'] as $attribute ) { |
|
546
|
|
|
$attribute_id = 0; |
|
547
|
|
|
$attribute_name = ''; |
|
548
|
|
|
|
|
549
|
|
|
// Check ID for global attributes or name for product attributes. |
|
550
|
|
|
if ( ! empty( $attribute['id'] ) ) { |
|
551
|
|
|
$attribute_id = absint( $attribute['id'] ); |
|
552
|
|
|
$attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); |
|
553
|
|
|
} elseif ( ! empty( $attribute['name'] ) ) { |
|
554
|
|
|
$attribute_name = sanitize_title( $attribute['name'] ); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
if ( ! $attribute_id && ! $attribute_name ) { |
|
558
|
|
|
continue; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
if ( ! isset( $parent_attributes[ $attribute_name ] ) || ! $parent_attributes[ $attribute_name ]->get_variation() ) { |
|
562
|
|
|
continue; |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
$attribute_key = sanitize_title( $parent_attributes[ $attribute_name ]->get_name() ); |
|
566
|
|
|
$attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; |
|
567
|
|
|
|
|
568
|
|
|
if ( $parent_attributes[ $attribute_name ]->is_taxonomy() ) { |
|
569
|
|
|
// If dealing with a taxonomy, we need to get the slug from the name posted to the API. |
|
570
|
|
|
$term = get_term_by( 'name', $attribute_value, $attribute_name ); |
|
571
|
|
|
|
|
572
|
|
|
if ( $term && ! is_wp_error( $term ) ) { |
|
573
|
|
|
$attribute_value = $term->slug; |
|
574
|
|
|
} else { |
|
575
|
|
|
$attribute_value = sanitize_title( $attribute_value ); |
|
576
|
|
|
} |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
$attributes[ $attribute_key ] = $attribute_value; |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
$variation->set_attributes( $attributes ); |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
// Menu order. |
|
586
|
|
|
if ( $request['menu_order'] ) { |
|
587
|
|
|
$variation->set_menu_order( $request['menu_order'] ); |
|
588
|
|
|
} |
|
589
|
|
|
|
|
590
|
|
|
// Meta data. |
|
591
|
|
|
if ( is_array( $request['meta_data'] ) ) { |
|
592
|
|
|
foreach ( $request['meta_data'] as $meta ) { |
|
593
|
|
|
$variation->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
|
|
|
|
|
|
594
|
|
|
} |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
/** |
|
598
|
|
|
* Filters an object before it is inserted via the REST API. |
|
599
|
|
|
* |
|
600
|
|
|
* The dynamic portion of the hook name, `$this->post_type`, |
|
601
|
|
|
* refers to the object type slug. |
|
602
|
|
|
* |
|
603
|
|
|
* @param \WC_Data $variation Object object. |
|
604
|
|
|
* @param \WP_REST_Request $request Request object. |
|
605
|
|
|
* @param bool $creating If is creating a new object. |
|
606
|
|
|
*/ |
|
607
|
|
|
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $variation, $request, $creating ); |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
/** |
|
611
|
|
|
* Clear caches here so in sync with any new variations. |
|
612
|
|
|
* |
|
613
|
|
|
* @param \WC_Data $object Object data. |
|
614
|
|
|
*/ |
|
615
|
|
|
public function clear_transients( $object ) { |
|
616
|
|
|
wc_delete_product_transients( $object->get_parent_id() ); |
|
617
|
|
|
wp_cache_delete( 'product-' . $object->get_parent_id(), 'products' ); |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
/** |
|
621
|
|
|
* Delete a variation. |
|
622
|
|
|
* |
|
623
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
|
624
|
|
|
* |
|
625
|
|
|
* @return bool|\WP_Error|\WP_REST_Response |
|
626
|
|
|
*/ |
|
627
|
|
|
public function delete_item( $request ) { |
|
628
|
|
|
$force = (bool) $request['force']; |
|
629
|
|
|
$object = $this->get_object( (int) $request['id'] ); |
|
630
|
|
|
$result = false; |
|
631
|
|
|
|
|
632
|
|
|
if ( ! $object || 0 === $object->get_id() ) { |
|
|
|
|
|
|
633
|
|
|
return new \WP_Error( |
|
634
|
|
|
"woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( |
|
635
|
|
|
'status' => 404, |
|
636
|
|
|
) |
|
637
|
|
|
); |
|
638
|
|
|
} |
|
639
|
|
|
|
|
640
|
|
|
$supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable( array( $object, 'get_status' ) ); |
|
641
|
|
|
|
|
642
|
|
|
/** |
|
643
|
|
|
* Filter whether an object is trashable. |
|
644
|
|
|
* |
|
645
|
|
|
* Return false to disable trash support for the object. |
|
646
|
|
|
* |
|
647
|
|
|
* @param boolean $supports_trash Whether the object type support trashing. |
|
648
|
|
|
* @param \WC_Data $object The object being considered for trashing support. |
|
649
|
|
|
*/ |
|
650
|
|
|
$supports_trash = apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object ); |
|
651
|
|
|
|
|
652
|
|
|
if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) { |
|
653
|
|
|
return new \WP_Error( |
|
654
|
|
|
/* translators: %s: post type */ |
|
655
|
|
|
"woocommerce_rest_user_cannot_delete_{$this->post_type}", sprintf( __( 'Sorry, you are not allowed to delete %s.', 'woocommerce' ), $this->post_type ), array( |
|
656
|
|
|
'status' => rest_authorization_required_code(), |
|
657
|
|
|
) |
|
658
|
|
|
); |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
$request->set_param( 'context', 'edit' ); |
|
662
|
|
|
|
|
663
|
|
|
// If we're forcing, then delete permanently. |
|
664
|
|
|
if ( $force ) { |
|
665
|
|
|
$previous = $this->prepare_object_for_response( $object, $request ); |
|
666
|
|
|
|
|
667
|
|
|
$object->delete( true ); |
|
668
|
|
|
|
|
669
|
|
|
$result = 0 === $object->get_id(); |
|
670
|
|
|
$response = new \WP_REST_Response(); |
|
671
|
|
|
$response->set_data( |
|
672
|
|
|
array( |
|
673
|
|
|
'deleted' => true, |
|
674
|
|
|
'previous' => $previous->get_data(), |
|
675
|
|
|
) |
|
676
|
|
|
); |
|
677
|
|
|
} else { |
|
678
|
|
|
// If we don't support trashing for this type, error out. |
|
679
|
|
|
if ( ! $supports_trash ) { |
|
680
|
|
|
return new \WP_Error( |
|
681
|
|
|
/* translators: %s: post type */ |
|
682
|
|
|
'woocommerce_rest_trash_not_supported', sprintf( __( 'The %s does not support trashing.', 'woocommerce' ), $this->post_type ), array( |
|
683
|
|
|
'status' => 501, |
|
684
|
|
|
) |
|
685
|
|
|
); |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
|
|
// Otherwise, only trash if we haven't already. |
|
689
|
|
|
if ( is_callable( array( $object, 'get_status' ) ) ) { |
|
690
|
|
|
if ( 'trash' === $object->get_status() ) { |
|
691
|
|
|
return new \WP_Error( |
|
692
|
|
|
/* translators: %s: post type */ |
|
693
|
|
|
'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( |
|
694
|
|
|
'status' => 410, |
|
695
|
|
|
) |
|
696
|
|
|
); |
|
697
|
|
|
} |
|
698
|
|
|
|
|
699
|
|
|
$object->delete(); |
|
700
|
|
|
$result = 'trash' === $object->get_status(); |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
$response = $this->prepare_object_for_response( $object, $request ); |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
|
|
if ( ! $result ) { |
|
707
|
|
|
return new \WP_Error( |
|
708
|
|
|
/* translators: %s: post type */ |
|
709
|
|
|
'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( |
|
710
|
|
|
'status' => 500, |
|
711
|
|
|
) |
|
712
|
|
|
); |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
/** |
|
716
|
|
|
* Fires after a single object is deleted or trashed via the REST API. |
|
717
|
|
|
* |
|
718
|
|
|
* @param \WC_Data $object The deleted or trashed object. |
|
719
|
|
|
* @param \WP_REST_Response $response The response data. |
|
720
|
|
|
* @param \WP_REST_Request $request The request sent to the API. |
|
721
|
|
|
*/ |
|
722
|
|
|
do_action( "woocommerce_rest_delete_{$this->post_type}_object", $object, $response, $request ); |
|
723
|
|
|
|
|
724
|
|
|
return $response; |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
/** |
|
728
|
|
|
* Get batch of items from requst. |
|
729
|
|
|
* |
|
730
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
|
731
|
|
|
* @param string $batch_type Batch type; one of create, update, delete. |
|
732
|
|
|
* @return array |
|
733
|
|
|
*/ |
|
734
|
|
|
protected function get_batch_of_items_from_request( $request, $batch_type ) { |
|
735
|
|
|
$params = $request->get_params(); |
|
736
|
|
|
$url_params = $request->get_url_params(); |
|
737
|
|
|
$product_id = $url_params['product_id']; |
|
738
|
|
|
|
|
739
|
|
|
if ( ! isset( $params[ $batch_type ] ) ) { |
|
740
|
|
|
return array(); |
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
$items = array_filter( $params[ $batch_type ] ); |
|
744
|
|
|
|
|
745
|
|
|
if ( 'update' === $batch_type || 'create' === $batch_type ) { |
|
746
|
|
|
foreach ( $items as $key => $item ) { |
|
747
|
|
|
$items[ $key ] = array_merge( |
|
748
|
|
|
array( |
|
749
|
|
|
'product_id' => $product_id, |
|
750
|
|
|
), |
|
751
|
|
|
$item |
|
752
|
|
|
); |
|
753
|
|
|
} |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
return array_filter( $items ); |
|
757
|
|
|
} |
|
758
|
|
|
|
|
759
|
|
|
/** |
|
760
|
|
|
* Prepare links for the request. |
|
761
|
|
|
* |
|
762
|
|
|
* @param \WC_Data $object Object data. |
|
763
|
|
|
* @param \WP_REST_Request $request Request object. |
|
764
|
|
|
* @return array Links for the given post. |
|
765
|
|
|
*/ |
|
766
|
|
|
protected function prepare_links( $object, $request ) { |
|
767
|
|
|
$product_id = (int) $request['product_id']; |
|
768
|
|
|
$base = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base ); |
|
769
|
|
|
$links = array( |
|
770
|
|
|
'self' => array( |
|
771
|
|
|
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $object->get_id() ) ), |
|
772
|
|
|
), |
|
773
|
|
|
'collection' => array( |
|
774
|
|
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ), |
|
775
|
|
|
), |
|
776
|
|
|
'up' => array( |
|
777
|
|
|
'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product_id ) ), |
|
778
|
|
|
), |
|
779
|
|
|
); |
|
780
|
|
|
return $links; |
|
781
|
|
|
} |
|
782
|
|
|
|
|
783
|
|
|
/** |
|
784
|
|
|
* Get the Variation's schema, conforming to JSON Schema. |
|
785
|
|
|
* |
|
786
|
|
|
* @return array |
|
787
|
|
|
*/ |
|
788
|
|
|
public function get_item_schema() { |
|
789
|
|
|
$weight_unit = get_option( 'woocommerce_weight_unit' ); |
|
790
|
|
|
$dimension_unit = get_option( 'woocommerce_dimension_unit' ); |
|
791
|
|
|
$schema = array( |
|
792
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
793
|
|
|
'title' => $this->post_type, |
|
794
|
|
|
'type' => 'object', |
|
795
|
|
|
'properties' => array( |
|
796
|
|
|
'id' => array( |
|
797
|
|
|
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
|
798
|
|
|
'type' => 'integer', |
|
799
|
|
|
'context' => array( 'view', 'edit' ), |
|
800
|
|
|
'readonly' => true, |
|
801
|
|
|
), |
|
802
|
|
|
'name' => array( |
|
803
|
|
|
'description' => __( 'Product parent name.', 'woocommerce' ), |
|
804
|
|
|
'type' => 'string', |
|
805
|
|
|
'context' => array( 'view', 'edit' ), |
|
806
|
|
|
), |
|
807
|
|
|
'type' => array( |
|
808
|
|
|
'description' => __( 'Product type.', 'woocommerce' ), |
|
809
|
|
|
'type' => 'string', |
|
810
|
|
|
'default' => 'variation', |
|
811
|
|
|
'enum' => array( 'variation' ), |
|
812
|
|
|
'context' => array( 'view', 'edit' ), |
|
813
|
|
|
), |
|
814
|
|
|
'parent_id' => array( |
|
815
|
|
|
'description' => __( 'Product parent ID.', 'woocommerce' ), |
|
816
|
|
|
'type' => 'integer', |
|
817
|
|
|
'context' => array( 'view', 'edit' ), |
|
818
|
|
|
), |
|
819
|
|
|
'date_created' => array( |
|
820
|
|
|
'description' => __( "The date the variation was created, in the site's timezone.", 'woocommerce' ), |
|
821
|
|
|
'type' => 'date-time', |
|
822
|
|
|
'context' => array( 'view', 'edit' ), |
|
823
|
|
|
'readonly' => true, |
|
824
|
|
|
), |
|
825
|
|
|
'date_modified' => array( |
|
826
|
|
|
'description' => __( "The date the variation was last modified, in the site's timezone.", 'woocommerce' ), |
|
827
|
|
|
'type' => 'date-time', |
|
828
|
|
|
'context' => array( 'view', 'edit' ), |
|
829
|
|
|
'readonly' => true, |
|
830
|
|
|
), |
|
831
|
|
|
'description' => array( |
|
832
|
|
|
'description' => __( 'Variation description.', 'woocommerce' ), |
|
833
|
|
|
'type' => 'string', |
|
834
|
|
|
'context' => array( 'view', 'edit' ), |
|
835
|
|
|
), |
|
836
|
|
|
'permalink' => array( |
|
837
|
|
|
'description' => __( 'Variation URL.', 'woocommerce' ), |
|
838
|
|
|
'type' => 'string', |
|
839
|
|
|
'format' => 'uri', |
|
840
|
|
|
'context' => array( 'view', 'edit' ), |
|
841
|
|
|
'readonly' => true, |
|
842
|
|
|
), |
|
843
|
|
|
'sku' => array( |
|
844
|
|
|
'description' => __( 'Unique identifier.', 'woocommerce' ), |
|
845
|
|
|
'type' => 'string', |
|
846
|
|
|
'context' => array( 'view', 'edit' ), |
|
847
|
|
|
), |
|
848
|
|
|
'price' => array( |
|
849
|
|
|
'description' => __( 'Current variation price.', 'woocommerce' ), |
|
850
|
|
|
'type' => 'string', |
|
851
|
|
|
'context' => array( 'view', 'edit' ), |
|
852
|
|
|
'readonly' => true, |
|
853
|
|
|
), |
|
854
|
|
|
'regular_price' => array( |
|
855
|
|
|
'description' => __( 'Variation regular price.', 'woocommerce' ), |
|
856
|
|
|
'type' => 'string', |
|
857
|
|
|
'context' => array( 'view', 'edit' ), |
|
858
|
|
|
), |
|
859
|
|
|
'sale_price' => array( |
|
860
|
|
|
'description' => __( 'Variation sale price.', 'woocommerce' ), |
|
861
|
|
|
'type' => 'string', |
|
862
|
|
|
'context' => array( 'view', 'edit' ), |
|
863
|
|
|
), |
|
864
|
|
|
'date_on_sale_from' => array( |
|
865
|
|
|
'description' => __( "Start date of sale price, in the site's timezone.", 'woocommerce' ), |
|
866
|
|
|
'type' => 'date-time', |
|
867
|
|
|
'context' => array( 'view', 'edit' ), |
|
868
|
|
|
), |
|
869
|
|
|
'date_on_sale_from_gmt' => array( |
|
870
|
|
|
'description' => __( 'Start date of sale price, as GMT.', 'woocommerce' ), |
|
871
|
|
|
'type' => 'date-time', |
|
872
|
|
|
'context' => array( 'view', 'edit' ), |
|
873
|
|
|
), |
|
874
|
|
|
'date_on_sale_to' => array( |
|
875
|
|
|
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ), |
|
876
|
|
|
'type' => 'date-time', |
|
877
|
|
|
'context' => array( 'view', 'edit' ), |
|
878
|
|
|
), |
|
879
|
|
|
'date_on_sale_to_gmt' => array( |
|
880
|
|
|
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ), |
|
881
|
|
|
'type' => 'date-time', |
|
882
|
|
|
'context' => array( 'view', 'edit' ), |
|
883
|
|
|
), |
|
884
|
|
|
'on_sale' => array( |
|
885
|
|
|
'description' => __( 'Shows if the variation is on sale.', 'woocommerce' ), |
|
886
|
|
|
'type' => 'boolean', |
|
887
|
|
|
'context' => array( 'view', 'edit' ), |
|
888
|
|
|
'readonly' => true, |
|
889
|
|
|
), |
|
890
|
|
|
'status' => array( |
|
891
|
|
|
'description' => __( 'Variation status.', 'woocommerce' ), |
|
892
|
|
|
'type' => 'string', |
|
893
|
|
|
'default' => 'publish', |
|
894
|
|
|
'enum' => array_keys( get_post_statuses() ), |
|
895
|
|
|
'context' => array( 'view', 'edit' ), |
|
896
|
|
|
), |
|
897
|
|
|
'purchasable' => array( |
|
898
|
|
|
'description' => __( 'Shows if the variation can be bought.', 'woocommerce' ), |
|
899
|
|
|
'type' => 'boolean', |
|
900
|
|
|
'context' => array( 'view', 'edit' ), |
|
901
|
|
|
'readonly' => true, |
|
902
|
|
|
), |
|
903
|
|
|
'virtual' => array( |
|
904
|
|
|
'description' => __( 'If the variation is virtual.', 'woocommerce' ), |
|
905
|
|
|
'type' => 'boolean', |
|
906
|
|
|
'default' => false, |
|
907
|
|
|
'context' => array( 'view', 'edit' ), |
|
908
|
|
|
), |
|
909
|
|
|
'downloadable' => array( |
|
910
|
|
|
'description' => __( 'If the variation is downloadable.', 'woocommerce' ), |
|
911
|
|
|
'type' => 'boolean', |
|
912
|
|
|
'default' => false, |
|
913
|
|
|
'context' => array( 'view', 'edit' ), |
|
914
|
|
|
), |
|
915
|
|
|
'downloads' => array( |
|
916
|
|
|
'description' => __( 'List of downloadable files.', 'woocommerce' ), |
|
917
|
|
|
'type' => 'array', |
|
918
|
|
|
'context' => array( 'view', 'edit' ), |
|
919
|
|
|
'items' => array( |
|
920
|
|
|
'type' => 'object', |
|
921
|
|
|
'properties' => array( |
|
922
|
|
|
'id' => array( |
|
923
|
|
|
'description' => __( 'File ID.', 'woocommerce' ), |
|
924
|
|
|
'type' => 'string', |
|
925
|
|
|
'context' => array( 'view', 'edit' ), |
|
926
|
|
|
), |
|
927
|
|
|
'name' => array( |
|
928
|
|
|
'description' => __( 'File name.', 'woocommerce' ), |
|
929
|
|
|
'type' => 'string', |
|
930
|
|
|
'context' => array( 'view', 'edit' ), |
|
931
|
|
|
), |
|
932
|
|
|
'file' => array( |
|
933
|
|
|
'description' => __( 'File URL.', 'woocommerce' ), |
|
934
|
|
|
'type' => 'string', |
|
935
|
|
|
'context' => array( 'view', 'edit' ), |
|
936
|
|
|
), |
|
937
|
|
|
), |
|
938
|
|
|
), |
|
939
|
|
|
), |
|
940
|
|
|
'download_limit' => array( |
|
941
|
|
|
'description' => __( 'Number of times downloadable files can be downloaded after purchase.', 'woocommerce' ), |
|
942
|
|
|
'type' => 'integer', |
|
943
|
|
|
'default' => -1, |
|
944
|
|
|
'context' => array( 'view', 'edit' ), |
|
945
|
|
|
), |
|
946
|
|
|
'download_expiry' => array( |
|
947
|
|
|
'description' => __( 'Number of days until access to downloadable files expires.', 'woocommerce' ), |
|
948
|
|
|
'type' => 'integer', |
|
949
|
|
|
'default' => -1, |
|
950
|
|
|
'context' => array( 'view', 'edit' ), |
|
951
|
|
|
), |
|
952
|
|
|
'tax_status' => array( |
|
953
|
|
|
'description' => __( 'Tax status.', 'woocommerce' ), |
|
954
|
|
|
'type' => 'string', |
|
955
|
|
|
'default' => 'taxable', |
|
956
|
|
|
'enum' => array( 'taxable', 'shipping', 'none' ), |
|
957
|
|
|
'context' => array( 'view', 'edit' ), |
|
958
|
|
|
), |
|
959
|
|
|
'tax_class' => array( |
|
960
|
|
|
'description' => __( 'Tax class.', 'woocommerce' ), |
|
961
|
|
|
'type' => 'string', |
|
962
|
|
|
'context' => array( 'view', 'edit' ), |
|
963
|
|
|
), |
|
964
|
|
|
'manage_stock' => array( |
|
965
|
|
|
'description' => __( 'Stock management at variation level.', 'woocommerce' ), |
|
966
|
|
|
'type' => 'boolean', |
|
967
|
|
|
'default' => false, |
|
968
|
|
|
'context' => array( 'view', 'edit' ), |
|
969
|
|
|
), |
|
970
|
|
|
'stock_quantity' => array( |
|
971
|
|
|
'description' => __( 'Stock quantity.', 'woocommerce' ), |
|
972
|
|
|
'type' => 'integer', |
|
973
|
|
|
'context' => array( 'view', 'edit' ), |
|
974
|
|
|
), |
|
975
|
|
|
'stock_status' => array( |
|
976
|
|
|
'description' => __( 'Controls the stock status of the product.', 'woocommerce' ), |
|
977
|
|
|
'type' => 'string', |
|
978
|
|
|
'default' => 'instock', |
|
979
|
|
|
'enum' => array_keys( wc_get_product_stock_status_options() ), |
|
980
|
|
|
'context' => array( 'view', 'edit' ), |
|
981
|
|
|
), |
|
982
|
|
|
'backorders' => array( |
|
983
|
|
|
'description' => __( 'If managing stock, this controls if backorders are allowed.', 'woocommerce' ), |
|
984
|
|
|
'type' => 'string', |
|
985
|
|
|
'default' => 'no', |
|
986
|
|
|
'enum' => array( 'no', 'notify', 'yes' ), |
|
987
|
|
|
'context' => array( 'view', 'edit' ), |
|
988
|
|
|
), |
|
989
|
|
|
'backorders_allowed' => array( |
|
990
|
|
|
'description' => __( 'Shows if backorders are allowed.', 'woocommerce' ), |
|
991
|
|
|
'type' => 'boolean', |
|
992
|
|
|
'context' => array( 'view', 'edit' ), |
|
993
|
|
|
'readonly' => true, |
|
994
|
|
|
), |
|
995
|
|
|
'backordered' => array( |
|
996
|
|
|
'description' => __( 'Shows if the variation is on backordered.', 'woocommerce' ), |
|
997
|
|
|
'type' => 'boolean', |
|
998
|
|
|
'context' => array( 'view', 'edit' ), |
|
999
|
|
|
'readonly' => true, |
|
1000
|
|
|
), |
|
1001
|
|
|
'weight' => array( |
|
1002
|
|
|
/* translators: %s: weight unit */ |
|
1003
|
|
|
'description' => sprintf( __( 'Variation weight (%s).', 'woocommerce' ), $weight_unit ), |
|
|
|
|
|
|
1004
|
|
|
'type' => 'string', |
|
1005
|
|
|
'context' => array( 'view', 'edit' ), |
|
1006
|
|
|
), |
|
1007
|
|
|
'dimensions' => array( |
|
1008
|
|
|
'description' => __( 'Variation dimensions.', 'woocommerce' ), |
|
1009
|
|
|
'type' => 'object', |
|
1010
|
|
|
'context' => array( 'view', 'edit' ), |
|
1011
|
|
|
'properties' => array( |
|
1012
|
|
|
'length' => array( |
|
1013
|
|
|
/* translators: %s: dimension unit */ |
|
1014
|
|
|
'description' => sprintf( __( 'Variation length (%s).', 'woocommerce' ), $dimension_unit ), |
|
1015
|
|
|
'type' => 'string', |
|
1016
|
|
|
'context' => array( 'view', 'edit' ), |
|
1017
|
|
|
), |
|
1018
|
|
|
'width' => array( |
|
1019
|
|
|
/* translators: %s: dimension unit */ |
|
1020
|
|
|
'description' => sprintf( __( 'Variation width (%s).', 'woocommerce' ), $dimension_unit ), |
|
1021
|
|
|
'type' => 'string', |
|
1022
|
|
|
'context' => array( 'view', 'edit' ), |
|
1023
|
|
|
), |
|
1024
|
|
|
'height' => array( |
|
1025
|
|
|
/* translators: %s: dimension unit */ |
|
1026
|
|
|
'description' => sprintf( __( 'Variation height (%s).', 'woocommerce' ), $dimension_unit ), |
|
1027
|
|
|
'type' => 'string', |
|
1028
|
|
|
'context' => array( 'view', 'edit' ), |
|
1029
|
|
|
), |
|
1030
|
|
|
), |
|
1031
|
|
|
), |
|
1032
|
|
|
'shipping_class' => array( |
|
1033
|
|
|
'description' => __( 'Shipping class slug.', 'woocommerce' ), |
|
1034
|
|
|
'type' => 'string', |
|
1035
|
|
|
'context' => array( 'view', 'edit' ), |
|
1036
|
|
|
), |
|
1037
|
|
|
'shipping_class_id' => array( |
|
1038
|
|
|
'description' => __( 'Shipping class ID.', 'woocommerce' ), |
|
1039
|
|
|
'type' => 'string', |
|
1040
|
|
|
'context' => array( 'view', 'edit' ), |
|
1041
|
|
|
'readonly' => true, |
|
1042
|
|
|
), |
|
1043
|
|
|
'image' => array( |
|
1044
|
|
|
'description' => __( 'Variation image data.', 'woocommerce' ), |
|
1045
|
|
|
'type' => 'object', |
|
1046
|
|
|
'context' => array( 'view', 'edit' ), |
|
1047
|
|
|
'properties' => array( |
|
1048
|
|
|
'id' => array( |
|
1049
|
|
|
'description' => __( 'Image ID.', 'woocommerce' ), |
|
1050
|
|
|
'type' => 'integer', |
|
1051
|
|
|
'context' => array( 'view', 'edit' ), |
|
1052
|
|
|
), |
|
1053
|
|
|
'date_created' => array( |
|
1054
|
|
|
'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ), |
|
1055
|
|
|
'type' => 'date-time', |
|
1056
|
|
|
'context' => array( 'view', 'edit' ), |
|
1057
|
|
|
'readonly' => true, |
|
1058
|
|
|
), |
|
1059
|
|
|
'date_created_gmt' => array( |
|
1060
|
|
|
'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ), |
|
1061
|
|
|
'type' => 'date-time', |
|
1062
|
|
|
'context' => array( 'view', 'edit' ), |
|
1063
|
|
|
'readonly' => true, |
|
1064
|
|
|
), |
|
1065
|
|
|
'date_modified' => array( |
|
1066
|
|
|
'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ), |
|
1067
|
|
|
'type' => 'date-time', |
|
1068
|
|
|
'context' => array( 'view', 'edit' ), |
|
1069
|
|
|
'readonly' => true, |
|
1070
|
|
|
), |
|
1071
|
|
|
'date_modified_gmt' => array( |
|
1072
|
|
|
'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ), |
|
1073
|
|
|
'type' => 'date-time', |
|
1074
|
|
|
'context' => array( 'view', 'edit' ), |
|
1075
|
|
|
'readonly' => true, |
|
1076
|
|
|
), |
|
1077
|
|
|
'src' => array( |
|
1078
|
|
|
'description' => __( 'Image URL.', 'woocommerce' ), |
|
1079
|
|
|
'type' => 'string', |
|
1080
|
|
|
'format' => 'uri', |
|
1081
|
|
|
'context' => array( 'view', 'edit' ), |
|
1082
|
|
|
), |
|
1083
|
|
|
'name' => array( |
|
1084
|
|
|
'description' => __( 'Image name.', 'woocommerce' ), |
|
1085
|
|
|
'type' => 'string', |
|
1086
|
|
|
'context' => array( 'view', 'edit' ), |
|
1087
|
|
|
), |
|
1088
|
|
|
'alt' => array( |
|
1089
|
|
|
'description' => __( 'Image alternative text.', 'woocommerce' ), |
|
1090
|
|
|
'type' => 'string', |
|
1091
|
|
|
'context' => array( 'view', 'edit' ), |
|
1092
|
|
|
), |
|
1093
|
|
|
), |
|
1094
|
|
|
), |
|
1095
|
|
|
'attributes' => array( |
|
1096
|
|
|
'description' => __( 'List of attributes.', 'woocommerce' ), |
|
1097
|
|
|
'type' => 'array', |
|
1098
|
|
|
'context' => array( 'view', 'edit' ), |
|
1099
|
|
|
'items' => array( |
|
1100
|
|
|
'type' => 'object', |
|
1101
|
|
|
'properties' => array( |
|
1102
|
|
|
'id' => array( |
|
1103
|
|
|
'description' => __( 'Attribute ID.', 'woocommerce' ), |
|
1104
|
|
|
'type' => 'integer', |
|
1105
|
|
|
'context' => array( 'view', 'edit' ), |
|
1106
|
|
|
), |
|
1107
|
|
|
'name' => array( |
|
1108
|
|
|
'description' => __( 'Attribute name.', 'woocommerce' ), |
|
1109
|
|
|
'type' => 'string', |
|
1110
|
|
|
'context' => array( 'view', 'edit' ), |
|
1111
|
|
|
), |
|
1112
|
|
|
'option' => array( |
|
1113
|
|
|
'description' => __( 'Selected attribute term name.', 'woocommerce' ), |
|
1114
|
|
|
'type' => 'string', |
|
1115
|
|
|
'context' => array( 'view', 'edit' ), |
|
1116
|
|
|
), |
|
1117
|
|
|
), |
|
1118
|
|
|
), |
|
1119
|
|
|
), |
|
1120
|
|
|
'menu_order' => array( |
|
1121
|
|
|
'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ), |
|
1122
|
|
|
'type' => 'integer', |
|
1123
|
|
|
'context' => array( 'view', 'edit' ), |
|
1124
|
|
|
), |
|
1125
|
|
|
'meta_data' => array( |
|
1126
|
|
|
'description' => __( 'Meta data.', 'woocommerce' ), |
|
1127
|
|
|
'type' => 'array', |
|
1128
|
|
|
'context' => array( 'view', 'edit' ), |
|
1129
|
|
|
'items' => array( |
|
1130
|
|
|
'type' => 'object', |
|
1131
|
|
|
'properties' => array( |
|
1132
|
|
|
'id' => array( |
|
1133
|
|
|
'description' => __( 'Meta ID.', 'woocommerce' ), |
|
1134
|
|
|
'type' => 'integer', |
|
1135
|
|
|
'context' => array( 'view', 'edit' ), |
|
1136
|
|
|
'readonly' => true, |
|
1137
|
|
|
), |
|
1138
|
|
|
'key' => array( |
|
1139
|
|
|
'description' => __( 'Meta key.', 'woocommerce' ), |
|
1140
|
|
|
'type' => 'string', |
|
1141
|
|
|
'context' => array( 'view', 'edit' ), |
|
1142
|
|
|
), |
|
1143
|
|
|
'value' => array( |
|
1144
|
|
|
'description' => __( 'Meta value.', 'woocommerce' ), |
|
1145
|
|
|
'type' => 'mixed', |
|
1146
|
|
|
'context' => array( 'view', 'edit' ), |
|
1147
|
|
|
), |
|
1148
|
|
|
), |
|
1149
|
|
|
), |
|
1150
|
|
|
), |
|
1151
|
|
|
), |
|
1152
|
|
|
); |
|
1153
|
|
|
return $this->add_additional_fields_schema( $schema ); |
|
1154
|
|
|
} |
|
1155
|
|
|
|
|
1156
|
|
|
/** |
|
1157
|
|
|
* Get the query params for collections of attachments. |
|
1158
|
|
|
* |
|
1159
|
|
|
* @return array |
|
1160
|
|
|
*/ |
|
1161
|
|
|
public function get_collection_params() { |
|
1162
|
|
|
$params = parent::get_collection_params(); |
|
1163
|
|
|
|
|
1164
|
|
|
unset( |
|
1165
|
|
|
$params['in_stock'], |
|
1166
|
|
|
$params['type'], |
|
1167
|
|
|
$params['featured'], |
|
1168
|
|
|
$params['category'], |
|
1169
|
|
|
$params['tag'], |
|
1170
|
|
|
$params['shipping_class'], |
|
1171
|
|
|
$params['attribute'], |
|
1172
|
|
|
$params['attribute_term'] |
|
1173
|
|
|
); |
|
1174
|
|
|
|
|
1175
|
|
|
$params['stock_status'] = array( |
|
1176
|
|
|
'description' => __( 'Limit result set to products with specified stock status.', 'woocommerce' ), |
|
1177
|
|
|
'type' => 'string', |
|
1178
|
|
|
'enum' => array_keys( wc_get_product_stock_status_options() ), |
|
1179
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
|
1180
|
|
|
'validate_callback' => 'rest_validate_request_arg', |
|
1181
|
|
|
); |
|
1182
|
|
|
|
|
1183
|
|
|
$params['search'] = array( |
|
1184
|
|
|
'description' => __( 'Search by similar product name or sku.', 'woocommerce' ), |
|
1185
|
|
|
'type' => 'string', |
|
1186
|
|
|
'validate_callback' => 'rest_validate_request_arg', |
|
1187
|
|
|
); |
|
1188
|
|
|
|
|
1189
|
|
|
return $params; |
|
1190
|
|
|
} |
|
1191
|
|
|
} |
|
1192
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.