1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* REST API Coupons controller |
4
|
|
|
* |
5
|
|
|
* Handles requests to the /coupons endpoint. |
6
|
|
|
* |
7
|
|
|
* @author WooThemes |
8
|
|
|
* @category API |
9
|
|
|
* @package WooCommerce/API |
10
|
|
|
* @since 2.6.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* REST API Coupons controller class. |
19
|
|
|
* |
20
|
|
|
* @package WooCommerce/API |
21
|
|
|
* @extends WC_REST_Posts_Controller |
22
|
|
|
*/ |
23
|
|
|
class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Endpoint namespace. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $namespace = 'wc/v1'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Route base. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $rest_base = 'coupons'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Post type. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $post_type = 'shop_coupon'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Order refunds actions. |
48
|
|
|
*/ |
49
|
|
|
public function __construct() { |
50
|
|
|
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Register the routes for coupons. |
55
|
|
|
*/ |
56
|
|
|
public function register_routes() { |
57
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
58
|
|
|
array( |
59
|
|
|
'methods' => WP_REST_Server::READABLE, |
60
|
|
|
'callback' => array( $this, 'get_items' ), |
61
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
62
|
|
|
'args' => $this->get_collection_params(), |
63
|
|
|
), |
64
|
|
|
array( |
65
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
66
|
|
|
'callback' => array( $this, 'create_item' ), |
67
|
|
|
'permission_callback' => array( $this, 'create_item_permissions_check' ), |
68
|
|
|
'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( |
69
|
|
|
'code' => array( |
70
|
|
|
'required' => true, |
71
|
|
|
), |
72
|
|
|
) ), |
73
|
|
|
), |
74
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
75
|
|
|
) ); |
76
|
|
|
|
77
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( |
78
|
|
|
array( |
79
|
|
|
'methods' => WP_REST_Server::READABLE, |
80
|
|
|
'callback' => array( $this, 'get_item' ), |
81
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
82
|
|
|
'args' => array( |
83
|
|
|
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
84
|
|
|
), |
85
|
|
|
), |
86
|
|
|
array( |
87
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
88
|
|
|
'callback' => array( $this, 'update_item' ), |
89
|
|
|
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
90
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
91
|
|
|
), |
92
|
|
|
array( |
93
|
|
|
'methods' => WP_REST_Server::DELETABLE, |
94
|
|
|
'callback' => array( $this, 'delete_item' ), |
95
|
|
|
'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
96
|
|
|
'args' => array( |
97
|
|
|
'force' => array( |
98
|
|
|
'default' => false, |
99
|
|
|
'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
100
|
|
|
), |
101
|
|
|
), |
102
|
|
|
), |
103
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
104
|
|
|
) ); |
105
|
|
|
|
106
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( |
107
|
|
|
array( |
108
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
109
|
|
|
'callback' => array( $this, 'batch_items' ), |
110
|
|
|
'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
111
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
112
|
|
|
), |
113
|
|
|
'schema' => array( $this, 'get_public_batch_schema' ), |
114
|
|
|
) ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Query args. |
119
|
|
|
* |
120
|
|
|
* @param array $args |
121
|
|
|
* @param WP_REST_Request $request |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function query_args( $args, $request ) { |
125
|
|
|
global $wpdb; |
126
|
|
|
|
127
|
|
|
if ( ! empty( $request['code'] ) ) { |
128
|
|
|
$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $request['code'] ) ); |
129
|
|
|
|
130
|
|
|
$args['post__in'] = array( $id ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $args; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Prepare a single coupon output for response. |
138
|
|
|
* |
139
|
|
|
* @param WP_Post $post Post object. |
140
|
|
|
* @param WP_REST_Request $request Request object. |
141
|
|
|
* @return WP_REST_Response $data |
142
|
|
|
*/ |
143
|
|
|
public function prepare_item_for_response( $post, $request ) { |
144
|
|
|
global $wpdb; |
145
|
|
|
|
146
|
|
|
// Get the coupon code. |
147
|
|
|
$code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $post->ID ) ); |
148
|
|
|
|
149
|
|
|
$coupon = new WC_Coupon( $code ); |
150
|
|
|
|
151
|
|
|
$data = array( |
152
|
|
|
'id' => $coupon->id, |
153
|
|
|
'code' => $coupon->code, |
154
|
|
|
'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt ), |
155
|
|
|
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt ), |
156
|
|
|
'discount_type' => $coupon->type, |
157
|
|
|
'description' => $post->post_excerpt, |
158
|
|
|
'amount' => wc_format_decimal( $coupon->coupon_amount, 2 ), |
159
|
|
|
'expiry_date' => ( ! empty( $coupon->expiry_date ) ) ? wc_rest_prepare_date_response( $coupon->expiry_date ) : null, |
160
|
|
|
'usage_count' => (int) $coupon->usage_count, |
161
|
|
|
'individual_use' => ( 'yes' === $coupon->individual_use ), |
162
|
|
|
'product_ids' => array_map( 'absint', (array) $coupon->product_ids ), |
163
|
|
|
'exclude_product_ids' => array_map( 'absint', (array) $coupon->exclude_product_ids ), |
164
|
|
|
'usage_limit' => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null, |
165
|
|
|
'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null, |
166
|
|
|
'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items, |
167
|
|
|
'free_shipping' => $coupon->enable_free_shipping(), |
168
|
|
|
'product_categories' => array_map( 'absint', (array) $coupon->product_categories ), |
169
|
|
|
'excluded_product_categories' => array_map( 'absint', (array) $coupon->exclude_product_categories ), |
170
|
|
|
'exclude_sale_items' => $coupon->exclude_sale_items(), |
171
|
|
|
'minimum_amount' => wc_format_decimal( $coupon->minimum_amount, 2 ), |
172
|
|
|
'maximum_amount' => wc_format_decimal( $coupon->maximum_amount, 2 ), |
173
|
|
|
'email_restrictions' => $coupon->customer_email, |
174
|
|
|
'used_by' => $coupon->get_used_by(), |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
178
|
|
|
$data = $this->add_additional_fields_to_object( $data, $request ); |
179
|
|
|
$data = $this->filter_response_by_context( $data, $context ); |
180
|
|
|
|
181
|
|
|
// Wrap the data in a response object. |
182
|
|
|
$response = rest_ensure_response( $data ); |
183
|
|
|
|
184
|
|
|
$response->add_links( $this->prepare_links( $post ) ); |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Filter the data for a response. |
188
|
|
|
* |
189
|
|
|
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being |
190
|
|
|
* prepared for the response. |
191
|
|
|
* |
192
|
|
|
* @param WP_REST_Response $response The response object. |
193
|
|
|
* @param WP_Post $post Post object. |
194
|
|
|
* @param WP_REST_Request $request Request object. |
195
|
|
|
*/ |
196
|
|
|
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Prepare a single coupon for create or update. |
201
|
|
|
* |
202
|
|
|
* @param WP_REST_Request $request Request object. |
203
|
|
|
* @return WP_Error|stdClass $data Post object. |
204
|
|
|
*/ |
205
|
|
|
protected function prepare_item_for_database( $request ) { |
206
|
|
|
global $wpdb; |
207
|
|
|
|
208
|
|
|
$data = new stdClass; |
209
|
|
|
|
210
|
|
|
// ID. |
211
|
|
|
if ( isset( $request['id'] ) ) { |
212
|
|
|
$data->ID = absint( $request['id'] ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$schema = $this->get_item_schema(); |
216
|
|
|
|
217
|
|
|
// Validate required POST fields. |
218
|
|
|
if ( 'POST' === $request->get_method() && empty( $data->ID ) ) { |
219
|
|
|
if ( empty( $request['code'] ) ) { |
220
|
|
|
return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) ); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// Code. |
225
|
|
|
if ( ! empty( $schema['properties']['code'] ) && ! empty( $request['code'] ) ) { |
226
|
|
|
$coupon_code = apply_filters( 'woocommerce_coupon_code', $request['code'] ); |
227
|
|
|
$id = isset( $data->ID ) ? $data->ID : 0; |
228
|
|
|
|
229
|
|
|
// Check for duplicate coupon codes. |
230
|
|
|
$coupon_found = $wpdb->get_var( $wpdb->prepare( " |
231
|
|
|
SELECT $wpdb->posts.ID |
232
|
|
|
FROM $wpdb->posts |
233
|
|
|
WHERE $wpdb->posts.post_type = 'shop_coupon' |
234
|
|
|
AND $wpdb->posts.post_status = 'publish' |
235
|
|
|
AND $wpdb->posts.post_title = '%s' |
236
|
|
|
AND $wpdb->posts.ID != %s |
237
|
|
|
", $coupon_code, $id ) ); |
238
|
|
|
|
239
|
|
|
if ( $coupon_found ) { |
240
|
|
|
return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) ); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$data->post_title = $coupon_code; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// Content. |
247
|
|
|
$data->post_content = ''; |
248
|
|
|
|
249
|
|
|
// Excerpt. |
250
|
|
|
if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['description'] ) ) { |
251
|
|
|
$data->post_excerpt = wp_filter_post_kses( $request['description'] ); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// Post type. |
255
|
|
|
$data->post_type = $this->post_type; |
256
|
|
|
|
257
|
|
|
// Post status. |
258
|
|
|
$data->post_status = 'publish'; |
259
|
|
|
|
260
|
|
|
// Comment status. |
261
|
|
|
$data->comment_status = 'closed'; |
262
|
|
|
|
263
|
|
|
// Ping status. |
264
|
|
|
$data->ping_status = 'closed'; |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Filter the query_vars used in `get_items` for the constructed query. |
268
|
|
|
* |
269
|
|
|
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being |
270
|
|
|
* prepared for insertion. |
271
|
|
|
* |
272
|
|
|
* @param stdClass $data An object representing a single item prepared |
273
|
|
|
* for inserting or updating the database. |
274
|
|
|
* @param WP_REST_Request $request Request object. |
275
|
|
|
*/ |
276
|
|
|
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Expiry date format. |
281
|
|
|
* |
282
|
|
|
* @param string $expiry_date |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
protected function get_coupon_expiry_date( $expiry_date ) { |
286
|
|
|
if ( '' != $expiry_date ) { |
287
|
|
|
return date( 'Y-m-d', strtotime( $expiry_date ) ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return ''; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Add post meta fields. |
295
|
|
|
* |
296
|
|
|
* @param WP_Post $post |
297
|
|
|
* @param WP_REST_Request $request |
298
|
|
|
* @return bool|WP_Error |
299
|
|
|
*/ |
300
|
|
|
protected function add_post_meta_fields( $post, $request ) { |
301
|
|
|
$data = array_filter( $request->get_params() ); |
302
|
|
|
|
303
|
|
|
$defaults = array( |
304
|
|
|
'discount_type' => 'fixed_cart', |
305
|
|
|
'amount' => 0, |
306
|
|
|
'individual_use' => false, |
307
|
|
|
'product_ids' => array(), |
308
|
|
|
'exclude_product_ids' => array(), |
309
|
|
|
'usage_limit' => '', |
310
|
|
|
'usage_limit_per_user' => '', |
311
|
|
|
'limit_usage_to_x_items' => '', |
312
|
|
|
'usage_count' => '', |
313
|
|
|
'expiry_date' => '', |
314
|
|
|
'free_shipping' => false, |
315
|
|
|
'product_categories' => array(), |
316
|
|
|
'excluded_product_categories' => array(), |
317
|
|
|
'exclude_sale_items' => false, |
318
|
|
|
'minimum_amount' => '', |
319
|
|
|
'maximum_amount' => '', |
320
|
|
|
'email_restrictions' => array(), |
321
|
|
|
'description' => '' |
322
|
|
|
); |
323
|
|
|
|
324
|
|
|
$data = wp_parse_args( $data, $defaults ); |
325
|
|
|
|
326
|
|
|
// Set coupon meta. |
327
|
|
|
update_post_meta( $post->ID, 'discount_type', $data['discount_type'] ); |
328
|
|
|
update_post_meta( $post->ID, 'coupon_amount', wc_format_decimal( $data['amount'] ) ); |
329
|
|
|
update_post_meta( $post->ID, 'individual_use', ( true === $data['individual_use'] ) ? 'yes' : 'no' ); |
330
|
|
|
update_post_meta( $post->ID, 'product_ids', implode( ',', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) ); |
331
|
|
|
update_post_meta( $post->ID, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) ); |
332
|
|
|
update_post_meta( $post->ID, 'usage_limit', absint( $data['usage_limit'] ) ); |
333
|
|
|
update_post_meta( $post->ID, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) ); |
334
|
|
|
update_post_meta( $post->ID, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) ); |
335
|
|
|
update_post_meta( $post->ID, 'usage_count', absint( $data['usage_count'] ) ); |
336
|
|
|
update_post_meta( $post->ID, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) ); |
337
|
|
|
update_post_meta( $post->ID, 'free_shipping', ( true === $data['free_shipping'] ) ? 'yes' : 'no' ); |
338
|
|
|
update_post_meta( $post->ID, 'product_categories', array_filter( array_map( 'intval', $data['product_categories'] ) ) ); |
339
|
|
|
update_post_meta( $post->ID, 'exclude_product_categories', array_filter( array_map( 'intval', $data['excluded_product_categories'] ) ) ); |
340
|
|
|
update_post_meta( $post->ID, 'exclude_sale_items', ( true === $data['exclude_sale_items'] ) ? 'yes' : 'no' ); |
341
|
|
|
update_post_meta( $post->ID, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) ); |
342
|
|
|
update_post_meta( $post->ID, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) ); |
343
|
|
|
update_post_meta( $post->ID, 'customer_email', array_filter( array_map( 'sanitize_email', $data['email_restrictions'] ) ) ); |
344
|
|
|
|
345
|
|
|
return true; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Update post meta fields. |
350
|
|
|
* |
351
|
|
|
* @param WP_Post $post |
352
|
|
|
* @param WP_REST_Request $request |
353
|
|
|
* @return bool|WP_Error |
354
|
|
|
*/ |
355
|
|
|
protected function update_post_meta_fields( $post, $request ) { |
356
|
|
|
if ( isset( $request['amount'] ) ) { |
357
|
|
|
update_post_meta( $post->ID, 'coupon_amount', wc_format_decimal( $request['amount'] ) ); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
View Code Duplication |
if ( isset( $request['individual_use'] ) ) { |
|
|
|
|
361
|
|
|
update_post_meta( $post->ID, 'individual_use', ( true === $request['individual_use'] ) ? 'yes' : 'no' ); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
View Code Duplication |
if ( isset( $request['product_ids'] ) ) { |
|
|
|
|
365
|
|
|
update_post_meta( $post->ID, 'product_ids', implode( ',', array_filter( array_map( 'intval', $request['product_ids'] ) ) ) ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
View Code Duplication |
if ( isset( $request['exclude_product_ids'] ) ) { |
|
|
|
|
369
|
|
|
update_post_meta( $post->ID, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $request['exclude_product_ids'] ) ) ) ); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
if ( isset( $request['usage_limit'] ) ) { |
373
|
|
|
update_post_meta( $post->ID, 'usage_limit', absint( $request['usage_limit'] ) ); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if ( isset( $request['usage_limit_per_user'] ) ) { |
377
|
|
|
update_post_meta( $post->ID, 'usage_limit_per_user', absint( $request['usage_limit_per_user'] ) ); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
if ( isset( $request['limit_usage_to_x_items'] ) ) { |
381
|
|
|
update_post_meta( $post->ID, 'limit_usage_to_x_items', absint( $request['limit_usage_to_x_items'] ) ); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
if ( isset( $request['usage_count'] ) ) { |
385
|
|
|
update_post_meta( $post->ID, 'usage_count', absint( $request['usage_count'] ) ); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
if ( isset( $request['expiry_date'] ) ) { |
389
|
|
|
update_post_meta( $post->ID, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $request['expiry_date'] ) ) ); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
View Code Duplication |
if ( isset( $request['free_shipping'] ) ) { |
|
|
|
|
393
|
|
|
update_post_meta( $post->ID, 'free_shipping', ( true === $request['free_shipping'] ) ? 'yes' : 'no' ); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
View Code Duplication |
if ( isset( $request['product_categories'] ) ) { |
|
|
|
|
397
|
|
|
update_post_meta( $post->ID, 'product_categories', array_filter( array_map( 'intval', $request['product_categories'] ) ) ); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
View Code Duplication |
if ( isset( $request['excluded_product_categories'] ) ) { |
|
|
|
|
401
|
|
|
update_post_meta( $post->ID, 'exclude_product_categories', array_filter( array_map( 'intval', $request['excluded_product_categories'] ) ) ); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
View Code Duplication |
if ( isset( $request['exclude_sale_items'] ) ) { |
|
|
|
|
405
|
|
|
update_post_meta( $post->ID, 'exclude_sale_items', ( true === $request['exclude_sale_items'] ) ? 'yes' : 'no' ); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
if ( isset( $request['minimum_amount'] ) ) { |
409
|
|
|
update_post_meta( $post->ID, 'minimum_amount', wc_format_decimal( $request['minimum_amount'] ) ); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
if ( isset( $request['maximum_amount'] ) ) { |
413
|
|
|
update_post_meta( $post->ID, 'maximum_amount', wc_format_decimal( $request['maximum_amount'] ) ); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
View Code Duplication |
if ( isset( $request['email_restrictions'] ) ) { |
|
|
|
|
417
|
|
|
update_post_meta( $post->ID, 'customer_email', array_filter( array_map( 'sanitize_email', $request['email_restrictions'] ) ) ); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
return true; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Get the Coupon's schema, conforming to JSON Schema. |
425
|
|
|
* |
426
|
|
|
* @return array |
427
|
|
|
*/ |
428
|
|
|
public function get_item_schema() { |
429
|
|
|
|
430
|
|
|
$schema = array( |
431
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
432
|
|
|
'title' => $this->post_type, |
433
|
|
|
'type' => 'object', |
434
|
|
|
'properties' => array( |
435
|
|
|
'id' => array( |
436
|
|
|
'description' => __( 'Unique identifier for the object.', 'woocommerce' ), |
437
|
|
|
'type' => 'integer', |
438
|
|
|
'context' => array( 'view', 'edit' ), |
439
|
|
|
'readonly' => true, |
440
|
|
|
), |
441
|
|
|
'code' => array( |
442
|
|
|
'description' => __( 'Coupon code.', 'woocommerce' ), |
443
|
|
|
'type' => 'string', |
444
|
|
|
'context' => array( 'view', 'edit' ), |
445
|
|
|
), |
446
|
|
|
'date_created' => array( |
447
|
|
|
'description' => __( "The date the coupon was created, in the site's timezone.", 'woocommerce' ), |
448
|
|
|
'type' => 'date-time', |
449
|
|
|
'context' => array( 'view', 'edit' ), |
450
|
|
|
'readonly' => true, |
451
|
|
|
), |
452
|
|
|
'date_modified' => array( |
453
|
|
|
'description' => __( "The date the coupon was last modified, in the site's timezone.", 'woocommerce' ), |
454
|
|
|
'type' => 'date-time', |
455
|
|
|
'context' => array( 'view', 'edit' ), |
456
|
|
|
'readonly' => true, |
457
|
|
|
), |
458
|
|
|
'description' => array( |
459
|
|
|
'description' => __( 'Coupon description.', 'woocommerce' ), |
460
|
|
|
'type' => 'string', |
461
|
|
|
'context' => array( 'view', 'edit' ), |
462
|
|
|
), |
463
|
|
|
'discount_type' => array( |
464
|
|
|
'description' => __( 'Determines the type of discount that will be applied.', 'woocommerce' ), |
465
|
|
|
'type' => 'string', |
466
|
|
|
'default' => 'fixed_cart', |
467
|
|
|
'enum' => array_keys( wc_get_coupon_types() ), |
468
|
|
|
'context' => array( 'view', 'edit' ), |
469
|
|
|
), |
470
|
|
|
'amount' => array( |
471
|
|
|
'description' => __( 'The amount of discount.', 'woocommerce' ), |
472
|
|
|
'type' => 'float', |
473
|
|
|
'context' => array( 'view', 'edit' ), |
474
|
|
|
), |
475
|
|
|
'expiry_date' => array( |
476
|
|
|
'description' => __( 'UTC DateTime when the coupon expires.', 'woocommerce' ), |
477
|
|
|
'type' => 'string', |
478
|
|
|
'context' => array( 'view', 'edit' ), |
479
|
|
|
), |
480
|
|
|
'usage_count' => array( |
481
|
|
|
'description' => __( 'Number of times the coupon has been used already.', 'woocommerce' ), |
482
|
|
|
'type' => 'integer', |
483
|
|
|
'context' => array( 'view', 'edit' ), |
484
|
|
|
'readonly' => true, |
485
|
|
|
), |
486
|
|
|
'individual_use' => array( |
487
|
|
|
'description' => __( 'Whether coupon can only be used individually.', 'woocommerce' ), |
488
|
|
|
'type' => 'boolean', |
489
|
|
|
'default' => false, |
490
|
|
|
'context' => array( 'view', 'edit' ), |
491
|
|
|
), |
492
|
|
|
'product_ids' => array( |
493
|
|
|
'description' => __( "List of product ID's the coupon can be used on.", 'woocommerce' ), |
494
|
|
|
'type' => 'array', |
495
|
|
|
'context' => array( 'view', 'edit' ), |
496
|
|
|
), |
497
|
|
|
'exclude_product_ids' => array( |
498
|
|
|
'description' => __( "List of product ID's the coupon cannot be used on.", 'woocommerce' ), |
499
|
|
|
'type' => 'array', |
500
|
|
|
'context' => array( 'view', 'edit' ), |
501
|
|
|
), |
502
|
|
|
'usage_limit' => array( |
503
|
|
|
'description' => __( 'How many times the coupon can be used.', 'woocommerce' ), |
504
|
|
|
'type' => 'integer', |
505
|
|
|
'context' => array( 'view', 'edit' ), |
506
|
|
|
), |
507
|
|
|
'usage_limit_per_user' => array( |
508
|
|
|
'description' => __( 'How many times the coupon can be used per customer.', 'woocommerce' ), |
509
|
|
|
'type' => 'integer', |
510
|
|
|
'context' => array( 'view', 'edit' ), |
511
|
|
|
), |
512
|
|
|
'limit_usage_to_x_items' => array( |
513
|
|
|
'description' => __( 'Max number of items in the cart the coupon can be applied to.', 'woocommerce' ), |
514
|
|
|
'type' => 'integer', |
515
|
|
|
'context' => array( 'view', 'edit' ), |
516
|
|
|
), |
517
|
|
|
'free_shipping' => array( |
518
|
|
|
'description' => __( 'Define if can be applied for free shipping.', 'woocommerce' ), |
519
|
|
|
'type' => 'boolean', |
520
|
|
|
'default' => false, |
521
|
|
|
'context' => array( 'view', 'edit' ), |
522
|
|
|
), |
523
|
|
|
'product_categories' => array( |
524
|
|
|
'description' => __( "List of category ID's the coupon applies to.", 'woocommerce' ), |
525
|
|
|
'type' => 'array', |
526
|
|
|
'context' => array( 'view', 'edit' ), |
527
|
|
|
), |
528
|
|
|
'excluded_product_categories' => array( |
529
|
|
|
'description' => __( "List of category ID's the coupon does not apply to.", 'woocommerce' ), |
530
|
|
|
'type' => 'array', |
531
|
|
|
'context' => array( 'view', 'edit' ), |
532
|
|
|
), |
533
|
|
|
'exclude_sale_items' => array( |
534
|
|
|
'description' => __( 'Define if should not apply when have sale items.', 'woocommerce' ), |
535
|
|
|
'type' => 'boolean', |
536
|
|
|
'default' => false, |
537
|
|
|
'context' => array( 'view', 'edit' ), |
538
|
|
|
), |
539
|
|
|
'minimum_amount' => array( |
540
|
|
|
'description' => __( 'Minimum order amount that needs to be in the cart before coupon applies.', 'woocommerce' ), |
541
|
|
|
'type' => 'float', |
542
|
|
|
'context' => array( 'view', 'edit' ), |
543
|
|
|
), |
544
|
|
|
'maximum_amount' => array( |
545
|
|
|
'description' => __( 'Maximum order amount allowed when using the coupon.', 'woocommerce' ), |
546
|
|
|
'type' => 'float', |
547
|
|
|
'context' => array( 'view', 'edit' ), |
548
|
|
|
), |
549
|
|
|
'email_restrictions' => array( |
550
|
|
|
'description' => __( 'List of email addresses that can use this coupon.', 'woocommerce' ), |
551
|
|
|
'type' => 'array', |
552
|
|
|
'context' => array( 'view', 'edit' ), |
553
|
|
|
), |
554
|
|
|
'used_by' => array( |
555
|
|
|
'description' => __( 'List of user IDs who have used the coupon.', 'woocommerce' ), |
556
|
|
|
'type' => 'array', |
557
|
|
|
'context' => array( 'view', 'edit' ), |
558
|
|
|
'readonly' => true, |
559
|
|
|
), |
560
|
|
|
), |
561
|
|
|
); |
562
|
|
|
|
563
|
|
|
return $this->add_additional_fields_schema( $schema );; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Get the query params for collections of attachments. |
568
|
|
|
* |
569
|
|
|
* @return array |
570
|
|
|
*/ |
571
|
|
View Code Duplication |
public function get_collection_params() { |
|
|
|
|
572
|
|
|
$params = parent::get_collection_params(); |
573
|
|
|
|
574
|
|
|
$params['code'] = array( |
575
|
|
|
'description' => __( 'Limit result set to resources with a specific code.', 'woocommerce' ), |
576
|
|
|
'type' => 'string', |
577
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
578
|
|
|
'validate_callback' => 'rest_validate_request_arg', |
579
|
|
|
); |
580
|
|
|
|
581
|
|
|
return $params; |
582
|
|
|
} |
583
|
|
|
} |
584
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.