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3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / legacy / api / v3 / class-wc-api-coupons.php
woocommerce / includes / legacy / api / v3 Last commit date
class-wc-api-authentication.php 5 years ago class-wc-api-coupons.php 5 years ago class-wc-api-customers.php 5 years ago class-wc-api-exception.php 5 years ago class-wc-api-json-handler.php 5 years ago class-wc-api-orders.php 4 years ago class-wc-api-products.php 3 years ago class-wc-api-reports.php 5 years ago class-wc-api-resource.php 4 years ago class-wc-api-server.php 5 years ago class-wc-api-taxes.php 4 years ago class-wc-api-webhooks.php 3 years ago interface-wc-api-handler.php 5 years ago
class-wc-api-coupons.php
577 lines
1 <?php
2 /**
3 * WooCommerce API Coupons Class
4 *
5 * Handles requests to the /coupons endpoint
6 *
7 * @author WooThemes
8 * @category API
9 * @package WooCommerce\RestApi
10 * @since 2.1
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 class WC_API_Coupons extends WC_API_Resource {
18
19 /** @var string $base the route base */
20 protected $base = '/coupons';
21
22 /**
23 * Register the routes for this class
24 *
25 * GET /coupons
26 * GET /coupons/count
27 * GET /coupons/<id>
28 *
29 * @since 2.1
30 * @param array $routes
31 * @return array
32 */
33 public function register_routes( $routes ) {
34
35 # GET/POST /coupons
36 $routes[ $this->base ] = array(
37 array( array( $this, 'get_coupons' ), WC_API_Server::READABLE ),
38 array( array( $this, 'create_coupon' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),
39 );
40
41 # GET /coupons/count
42 $routes[ $this->base . '/count' ] = array(
43 array( array( $this, 'get_coupons_count' ), WC_API_Server::READABLE ),
44 );
45
46 # GET/PUT/DELETE /coupons/<id>
47 $routes[ $this->base . '/(?P<id>\d+)' ] = array(
48 array( array( $this, 'get_coupon' ), WC_API_Server::READABLE ),
49 array( array( $this, 'edit_coupon' ), WC_API_SERVER::EDITABLE | WC_API_SERVER::ACCEPT_DATA ),
50 array( array( $this, 'delete_coupon' ), WC_API_SERVER::DELETABLE ),
51 );
52
53 # GET /coupons/code/<code>, note that coupon codes can contain spaces, dashes and underscores
54 $routes[ $this->base . '/code/(?P<code>\w[\w\s\-]*)' ] = array(
55 array( array( $this, 'get_coupon_by_code' ), WC_API_Server::READABLE ),
56 );
57
58 # POST|PUT /coupons/bulk
59 $routes[ $this->base . '/bulk' ] = array(
60 array( array( $this, 'bulk' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
61 );
62
63 return $routes;
64 }
65
66 /**
67 * Get all coupons
68 *
69 * @since 2.1
70 * @param string $fields
71 * @param array $filter
72 * @param int $page
73 * @return array
74 */
75 public function get_coupons( $fields = null, $filter = array(), $page = 1 ) {
76
77 $filter['page'] = $page;
78
79 $query = $this->query_coupons( $filter );
80
81 $coupons = array();
82
83 foreach ( $query->posts as $coupon_id ) {
84
85 if ( ! $this->is_readable( $coupon_id ) ) {
86 continue;
87 }
88
89 $coupons[] = current( $this->get_coupon( $coupon_id, $fields ) );
90 }
91
92 $this->server->add_pagination_headers( $query );
93
94 return array( 'coupons' => $coupons );
95 }
96
97 /**
98 * Get the coupon for the given ID
99 *
100 * @since 2.1
101 * @param int $id the coupon ID
102 * @param string $fields fields to include in response
103 * @return array|WP_Error
104 */
105 public function get_coupon( $id, $fields = null ) {
106 try {
107
108 $id = $this->validate_request( $id, 'shop_coupon', 'read' );
109
110 if ( is_wp_error( $id ) ) {
111 return $id;
112 }
113
114 $coupon = new WC_Coupon( $id );
115
116 if ( 0 === $coupon->get_id() ) {
117 throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), 404 );
118 }
119
120 $coupon_data = array(
121 'id' => $coupon->get_id(),
122 'code' => $coupon->get_code(),
123 'type' => $coupon->get_discount_type(),
124 'created_at' => $this->server->format_datetime( $coupon->get_date_created() ? $coupon->get_date_created()->getTimestamp() : 0 ), // API gives UTC times.
125 'updated_at' => $this->server->format_datetime( $coupon->get_date_modified() ? $coupon->get_date_modified()->getTimestamp() : 0 ), // API gives UTC times.
126 'amount' => wc_format_decimal( $coupon->get_amount(), 2 ),
127 'individual_use' => $coupon->get_individual_use(),
128 'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
129 'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
130 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
131 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
132 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
133 'usage_count' => (int) $coupon->get_usage_count(),
134 'expiry_date' => $coupon->get_date_expires() ? $this->server->format_datetime( $coupon->get_date_expires()->getTimestamp() ) : null, // API gives UTC times.
135 'enable_free_shipping' => $coupon->get_free_shipping(),
136 'product_category_ids' => array_map( 'absint', (array) $coupon->get_product_categories() ),
137 'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),
138 'exclude_sale_items' => $coupon->get_exclude_sale_items(),
139 'minimum_amount' => wc_format_decimal( $coupon->get_minimum_amount(), 2 ),
140 'maximum_amount' => wc_format_decimal( $coupon->get_maximum_amount(), 2 ),
141 'customer_emails' => $coupon->get_email_restrictions(),
142 'description' => $coupon->get_description(),
143 );
144
145 return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) );
146 } catch ( WC_API_Exception $e ) {
147 return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
148 }
149 }
150
151 /**
152 * Get the total number of coupons
153 *
154 * @since 2.1
155 * @param array $filter
156 * @return array|WP_Error
157 */
158 public function get_coupons_count( $filter = array() ) {
159 try {
160 if ( ! current_user_can( 'read_private_shop_coupons' ) ) {
161 throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), 401 );
162 }
163
164 $query = $this->query_coupons( $filter );
165
166 return array( 'count' => (int) $query->found_posts );
167 } catch ( WC_API_Exception $e ) {
168 return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
169 }
170 }
171
172 /**
173 * Get the coupon for the given code
174 *
175 * @since 2.1
176 * @param string $code the coupon code
177 * @param string $fields fields to include in response
178 * @return int|WP_Error
179 */
180 public function get_coupon_by_code( $code, $fields = null ) {
181 global $wpdb;
182
183 try {
184 $id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1;", $code ) );
185
186 if ( is_null( $id ) ) {
187 throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), 404 );
188 }
189
190 return $this->get_coupon( $id, $fields );
191 } catch ( WC_API_Exception $e ) {
192 return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
193 }
194 }
195
196 /**
197 * Create a coupon
198 *
199 * @since 2.2
200 *
201 * @param array $data
202 *
203 * @return array|WP_Error
204 */
205 public function create_coupon( $data ) {
206 global $wpdb;
207
208 try {
209 if ( ! isset( $data['coupon'] ) ) {
210 throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'coupon' ), 400 );
211 }
212
213 $data = $data['coupon'];
214
215 // Check user permission
216 if ( ! current_user_can( 'publish_shop_coupons' ) ) {
217 throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_coupon', __( 'You do not have permission to create coupons', 'woocommerce' ), 401 );
218 }
219
220 $data = apply_filters( 'woocommerce_api_create_coupon_data', $data, $this );
221
222 // Check if coupon code is specified
223 if ( ! isset( $data['code'] ) ) {
224 throw new WC_API_Exception( 'woocommerce_api_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ), 400 );
225 }
226
227 $coupon_code = wc_format_coupon_code( $data['code'] );
228 $id_from_code = wc_get_coupon_id_by_code( $coupon_code );
229
230 if ( $id_from_code ) {
231 throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
232 }
233
234 $defaults = array(
235 'type' => 'fixed_cart',
236 'amount' => 0,
237 'individual_use' => false,
238 'product_ids' => array(),
239 'exclude_product_ids' => array(),
240 'usage_limit' => '',
241 'usage_limit_per_user' => '',
242 'limit_usage_to_x_items' => '',
243 'usage_count' => '',
244 'expiry_date' => '',
245 'enable_free_shipping' => false,
246 'product_category_ids' => array(),
247 'exclude_product_category_ids' => array(),
248 'exclude_sale_items' => false,
249 'minimum_amount' => '',
250 'maximum_amount' => '',
251 'customer_emails' => array(),
252 'description' => '',
253 );
254
255 $coupon_data = wp_parse_args( $data, $defaults );
256
257 // Validate coupon types
258 if ( ! in_array( wc_clean( $coupon_data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
259 throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ), 400 );
260 }
261
262 $new_coupon = array(
263 'post_title' => $coupon_code,
264 'post_content' => '',
265 'post_status' => 'publish',
266 'post_author' => get_current_user_id(),
267 'post_type' => 'shop_coupon',
268 'post_excerpt' => $coupon_data['description'],
269 );
270
271 $id = wp_insert_post( $new_coupon, true );
272
273 if ( is_wp_error( $id ) ) {
274 throw new WC_API_Exception( 'woocommerce_api_cannot_create_coupon', $id->get_error_message(), 400 );
275 }
276
277 // Set coupon meta
278 update_post_meta( $id, 'discount_type', $coupon_data['type'] );
279 update_post_meta( $id, 'coupon_amount', wc_format_decimal( $coupon_data['amount'] ) );
280 update_post_meta( $id, 'individual_use', ( true === $coupon_data['individual_use'] ) ? 'yes' : 'no' );
281 update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['product_ids'] ) ) ) );
282 update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['exclude_product_ids'] ) ) ) );
283 update_post_meta( $id, 'usage_limit', absint( $coupon_data['usage_limit'] ) );
284 update_post_meta( $id, 'usage_limit_per_user', absint( $coupon_data['usage_limit_per_user'] ) );
285 update_post_meta( $id, 'limit_usage_to_x_items', absint( $coupon_data['limit_usage_to_x_items'] ) );
286 update_post_meta( $id, 'usage_count', absint( $coupon_data['usage_count'] ) );
287 update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ) ) );
288 update_post_meta( $id, 'date_expires', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ), true ) );
289 update_post_meta( $id, 'free_shipping', ( true === $coupon_data['enable_free_shipping'] ) ? 'yes' : 'no' );
290 update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $coupon_data['product_category_ids'] ) ) );
291 update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $coupon_data['exclude_product_category_ids'] ) ) );
292 update_post_meta( $id, 'exclude_sale_items', ( true === $coupon_data['exclude_sale_items'] ) ? 'yes' : 'no' );
293 update_post_meta( $id, 'minimum_amount', wc_format_decimal( $coupon_data['minimum_amount'] ) );
294 update_post_meta( $id, 'maximum_amount', wc_format_decimal( $coupon_data['maximum_amount'] ) );
295 update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $coupon_data['customer_emails'] ) ) );
296
297 do_action( 'woocommerce_api_create_coupon', $id, $data );
298 do_action( 'woocommerce_new_coupon', $id );
299
300 $this->server->send_status( 201 );
301
302 return $this->get_coupon( $id );
303 } catch ( WC_API_Exception $e ) {
304 return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
305 }
306 }
307
308 /**
309 * Edit a coupon
310 *
311 * @since 2.2
312 *
313 * @param int $id the coupon ID
314 * @param array $data
315 *
316 * @return array|WP_Error
317 */
318 public function edit_coupon( $id, $data ) {
319
320 try {
321 if ( ! isset( $data['coupon'] ) ) {
322 throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'coupon' ), 400 );
323 }
324
325 $data = $data['coupon'];
326
327 $id = $this->validate_request( $id, 'shop_coupon', 'edit' );
328
329 if ( is_wp_error( $id ) ) {
330 return $id;
331 }
332
333 $data = apply_filters( 'woocommerce_api_edit_coupon_data', $data, $id, $this );
334
335 if ( isset( $data['code'] ) ) {
336 global $wpdb;
337
338 $coupon_code = wc_format_coupon_code( $data['code'] );
339 $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
340
341 if ( $id_from_code ) {
342 throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
343 }
344
345 $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_title' => $coupon_code ) );
346
347 if ( 0 === $updated ) {
348 throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 );
349 }
350 }
351
352 if ( isset( $data['description'] ) ) {
353 $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_excerpt' => $data['description'] ) );
354
355 if ( 0 === $updated ) {
356 throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 );
357 }
358 }
359
360 if ( isset( $data['type'] ) ) {
361 // Validate coupon types
362 if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
363 throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_type', sprintf( __( 'Invalid coupon type - the coupon type must be any of these: %s', 'woocommerce' ), implode( ', ', array_keys( wc_get_coupon_types() ) ) ), 400 );
364 }
365 update_post_meta( $id, 'discount_type', $data['type'] );
366 }
367
368 if ( isset( $data['amount'] ) ) {
369 update_post_meta( $id, 'coupon_amount', wc_format_decimal( $data['amount'] ) );
370 }
371
372 if ( isset( $data['individual_use'] ) ) {
373 update_post_meta( $id, 'individual_use', ( true === $data['individual_use'] ) ? 'yes' : 'no' );
374 }
375
376 if ( isset( $data['product_ids'] ) ) {
377 update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) );
378 }
379
380 if ( isset( $data['exclude_product_ids'] ) ) {
381 update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) );
382 }
383
384 if ( isset( $data['usage_limit'] ) ) {
385 update_post_meta( $id, 'usage_limit', absint( $data['usage_limit'] ) );
386 }
387
388 if ( isset( $data['usage_limit_per_user'] ) ) {
389 update_post_meta( $id, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) );
390 }
391
392 if ( isset( $data['limit_usage_to_x_items'] ) ) {
393 update_post_meta( $id, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) );
394 }
395
396 if ( isset( $data['usage_count'] ) ) {
397 update_post_meta( $id, 'usage_count', absint( $data['usage_count'] ) );
398 }
399
400 if ( isset( $data['expiry_date'] ) ) {
401 update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) );
402 update_post_meta( $id, 'date_expires', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ), true ) );
403 }
404
405 if ( isset( $data['enable_free_shipping'] ) ) {
406 update_post_meta( $id, 'free_shipping', ( true === $data['enable_free_shipping'] ) ? 'yes' : 'no' );
407 }
408
409 if ( isset( $data['product_category_ids'] ) ) {
410 update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $data['product_category_ids'] ) ) );
411 }
412
413 if ( isset( $data['exclude_product_category_ids'] ) ) {
414 update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $data['exclude_product_category_ids'] ) ) );
415 }
416
417 if ( isset( $data['exclude_sale_items'] ) ) {
418 update_post_meta( $id, 'exclude_sale_items', ( true === $data['exclude_sale_items'] ) ? 'yes' : 'no' );
419 }
420
421 if ( isset( $data['minimum_amount'] ) ) {
422 update_post_meta( $id, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) );
423 }
424
425 if ( isset( $data['maximum_amount'] ) ) {
426 update_post_meta( $id, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) );
427 }
428
429 if ( isset( $data['customer_emails'] ) ) {
430 update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $data['customer_emails'] ) ) );
431 }
432
433 do_action( 'woocommerce_api_edit_coupon', $id, $data );
434 do_action( 'woocommerce_update_coupon', $id );
435
436 return $this->get_coupon( $id );
437 } catch ( WC_API_Exception $e ) {
438 return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
439 }
440 }
441
442 /**
443 * Delete a coupon
444 *
445 * @since 2.2
446 *
447 * @param int $id the coupon ID
448 * @param bool $force true to permanently delete coupon, false to move to trash
449 *
450 * @return array|int|WP_Error
451 */
452 public function delete_coupon( $id, $force = false ) {
453
454 $id = $this->validate_request( $id, 'shop_coupon', 'delete' );
455
456 if ( is_wp_error( $id ) ) {
457 return $id;
458 }
459
460 do_action( 'woocommerce_api_delete_coupon', $id, $this );
461
462 return $this->delete( $id, 'shop_coupon', ( 'true' === $force ) );
463 }
464
465 /**
466 * expiry_date format
467 *
468 * @since 2.3.0
469 * @param string $expiry_date
470 * @param bool $as_timestamp (default: false)
471 * @return string|int
472 */
473 protected function get_coupon_expiry_date( $expiry_date, $as_timestamp = false ) {
474 if ( '' != $expiry_date ) {
475 if ( $as_timestamp ) {
476 return strtotime( $expiry_date );
477 }
478
479 return date( 'Y-m-d', strtotime( $expiry_date ) );
480 }
481
482 return '';
483 }
484
485 /**
486 * Helper method to get coupon post objects
487 *
488 * @since 2.1
489 * @param array $args request arguments for filtering query
490 * @return WP_Query
491 */
492 private function query_coupons( $args ) {
493
494 // set base query arguments
495 $query_args = array(
496 'fields' => 'ids',
497 'post_type' => 'shop_coupon',
498 'post_status' => 'publish',
499 );
500
501 $query_args = $this->merge_query_args( $query_args, $args );
502
503 return new WP_Query( $query_args );
504 }
505
506 /**
507 * Bulk update or insert coupons
508 * Accepts an array with coupons in the formats supported by
509 * WC_API_Coupons->create_coupon() and WC_API_Coupons->edit_coupon()
510 *
511 * @since 2.4.0
512 *
513 * @param array $data
514 *
515 * @return array|WP_Error
516 */
517 public function bulk( $data ) {
518
519 try {
520 if ( ! isset( $data['coupons'] ) ) {
521 throw new WC_API_Exception( 'woocommerce_api_missing_coupons_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'coupons' ), 400 );
522 }
523
524 $data = $data['coupons'];
525 $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'coupons' );
526
527 // Limit bulk operation
528 if ( count( $data ) > $limit ) {
529 throw new WC_API_Exception( 'woocommerce_api_coupons_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), 413 );
530 }
531
532 $coupons = array();
533
534 foreach ( $data as $_coupon ) {
535 $coupon_id = 0;
536
537 // Try to get the coupon ID
538 if ( isset( $_coupon['id'] ) ) {
539 $coupon_id = intval( $_coupon['id'] );
540 }
541
542 if ( $coupon_id ) {
543
544 // Coupon exists / edit coupon
545 $edit = $this->edit_coupon( $coupon_id, array( 'coupon' => $_coupon ) );
546
547 if ( is_wp_error( $edit ) ) {
548 $coupons[] = array(
549 'id' => $coupon_id,
550 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),
551 );
552 } else {
553 $coupons[] = $edit['coupon'];
554 }
555 } else {
556
557 // Coupon don't exists / create coupon
558 $new = $this->create_coupon( array( 'coupon' => $_coupon ) );
559
560 if ( is_wp_error( $new ) ) {
561 $coupons[] = array(
562 'id' => $coupon_id,
563 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),
564 );
565 } else {
566 $coupons[] = $new['coupon'];
567 }
568 }
569 }
570
571 return array( 'coupons' => apply_filters( 'woocommerce_api_coupons_bulk_response', $coupons, $this ) );
572 } catch ( WC_API_Exception $e ) {
573 return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
574 }
575 }
576 }
577