/includes/api/legacy/v3/class-wc-api-coupons.php

  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/API 
  10. * @since 2.1 
  11. */ 
  12.  
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; // Exit if accessed directly 
  15.  
  16. class WC_API_Coupons extends WC_API_Resource { 
  17.  
  18. /** @var string $base the route base */ 
  19. protected $base = '/coupons'; 
  20.  
  21. /** 
  22. * Register the routes for this class 
  23. * 
  24. * GET /coupons 
  25. * GET /coupons/count 
  26. * GET /coupons/<id> 
  27. * 
  28. * @since 2.1 
  29. * @param array $routes 
  30. * @return array 
  31. */ 
  32. public function register_routes( $routes ) { 
  33.  
  34. # GET/POST /coupons 
  35. $routes[ $this->base ] = array( 
  36. array( array( $this, 'get_coupons' ), WC_API_Server::READABLE ),  
  37. array( array( $this, 'create_coupon' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  38. ); 
  39.  
  40. # GET /coupons/count 
  41. $routes[ $this->base . '/count' ] = array( 
  42. array( array( $this, 'get_coupons_count' ), WC_API_Server::READABLE ),  
  43. ); 
  44.  
  45. # GET/PUT/DELETE /coupons/<id> 
  46. $routes[ $this->base . '/(?P<id>\d+)' ] = array( 
  47. array( array( $this, 'get_coupon' ), WC_API_Server::READABLE ),  
  48. array( array( $this, 'edit_coupon' ), WC_API_SERVER::EDITABLE | WC_API_SERVER::ACCEPT_DATA ),  
  49. array( array( $this, 'delete_coupon' ), WC_API_SERVER::DELETABLE ),  
  50. ); 
  51.  
  52. # GET /coupons/code/<code>, note that coupon codes can contain spaces, dashes and underscores 
  53. $routes[ $this->base . '/code/(?P<code>\w[\w\s\-]*)' ] = array( 
  54. array( array( $this, 'get_coupon_by_code' ), WC_API_Server::READABLE ),  
  55. ); 
  56.  
  57. # POST|PUT /coupons/bulk 
  58. $routes[ $this->base . '/bulk' ] = array( 
  59. array( array( $this, 'bulk' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  60. ); 
  61.  
  62. return $routes; 
  63.  
  64. /** 
  65. * Get all coupons 
  66. * 
  67. * @since 2.1 
  68. * @param string $fields 
  69. * @param array $filter 
  70. * @param int $page 
  71. * @return array 
  72. */ 
  73. public function get_coupons( $fields = null, $filter = array(), $page = 1 ) { 
  74.  
  75. $filter['page'] = $page; 
  76.  
  77. $query = $this->query_coupons( $filter ); 
  78.  
  79. $coupons = array(); 
  80.  
  81. foreach ( $query->posts as $coupon_id ) { 
  82.  
  83. if ( ! $this->is_readable( $coupon_id ) ) { 
  84. continue; 
  85.  
  86. $coupons[] = current( $this->get_coupon( $coupon_id, $fields ) ); 
  87.  
  88. $this->server->add_pagination_headers( $query ); 
  89.  
  90. return array( 'coupons' => $coupons ); 
  91.  
  92. /** 
  93. * Get the coupon for the given ID 
  94. * 
  95. * @since 2.1 
  96. * @param int $id the coupon ID 
  97. * @param string $fields fields to include in response 
  98. * @return array|WP_Error 
  99. */ 
  100. public function get_coupon( $id, $fields = null ) { 
  101. try { 
  102.  
  103. $id = $this->validate_request( $id, 'shop_coupon', 'read' ); 
  104.  
  105. if ( is_wp_error( $id ) ) { 
  106. return $id; 
  107.  
  108. $coupon = new WC_Coupon( $id ); 
  109.  
  110. if ( 0 === $coupon->get_id() ) { 
  111. throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), 404 ); 
  112.  
  113. $coupon_data = array( 
  114. 'id' => $coupon->get_id(),  
  115. 'code' => $coupon->get_code(),  
  116. 'type' => $coupon->get_discount_type(),  
  117. 'created_at' => $this->server->format_datetime( $coupon->get_date_created() ? $coupon->get_date_created()->getTimestamp() : 0 ), // API gives UTC times. 
  118. 'updated_at' => $this->server->format_datetime( $coupon->get_date_modified() ? $coupon->get_date_modified()->getTimestamp() : 0 ), // API gives UTC times. 
  119. 'amount' => wc_format_decimal( $coupon->get_amount(), 2 ),  
  120. 'individual_use' => $coupon->get_individual_use(),  
  121. 'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),  
  122. 'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),  
  123. 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,  
  124. 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,  
  125. 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),  
  126. 'usage_count' => (int) $coupon->get_usage_count(),  
  127. 'expiry_date' => $coupon->get_date_expires() ? $this->server->format_datetime( $coupon->get_date_expires()->getTimestamp() ) : null, // API gives UTC times. 
  128. 'enable_free_shipping' => $coupon->get_free_shipping(),  
  129. 'product_category_ids' => array_map( 'absint', (array) $coupon->get_product_categories() ),  
  130. 'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),  
  131. 'exclude_sale_items' => $coupon->get_exclude_sale_items(),  
  132. 'minimum_amount' => wc_format_decimal( $coupon->get_minimum_amount(), 2 ),  
  133. 'maximum_amount' => wc_format_decimal( $coupon->get_maximum_amount(), 2 ),  
  134. 'customer_emails' => $coupon->get_email_restrictions(),  
  135. 'description' => $coupon->get_description(),  
  136. ); 
  137.  
  138. return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) ); 
  139. } catch ( WC_API_Exception $e ) { 
  140. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  141.  
  142. /** 
  143. * Get the total number of coupons 
  144. * 
  145. * @since 2.1 
  146. * @param array $filter 
  147. * @return array 
  148. */ 
  149. public function get_coupons_count( $filter = array() ) { 
  150. try { 
  151. if ( ! current_user_can( 'read_private_shop_coupons' ) ) { 
  152. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), 401 ); 
  153.  
  154. $query = $this->query_coupons( $filter ); 
  155.  
  156. return array( 'count' => (int) $query->found_posts ); 
  157. } catch ( WC_API_Exception $e ) { 
  158. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  159.  
  160. /** 
  161. * Get the coupon for the given code 
  162. * 
  163. * @since 2.1 
  164. * @param string $code the coupon code 
  165. * @param string $fields fields to include in response 
  166. * @return int|WP_Error 
  167. */ 
  168. public function get_coupon_by_code( $code, $fields = null ) { 
  169. global $wpdb; 
  170.  
  171. try { 
  172. $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 ) ); 
  173.  
  174. if ( is_null( $id ) ) { 
  175. throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), 404 ); 
  176.  
  177. return $this->get_coupon( $id, $fields ); 
  178. } catch ( WC_API_Exception $e ) { 
  179. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  180.  
  181. /** 
  182. * Create a coupon 
  183. * 
  184. * @since 2.2 
  185. * @param array $data 
  186. * @return array 
  187. */ 
  188. public function create_coupon( $data ) { 
  189. global $wpdb; 
  190.  
  191. try { 
  192. if ( ! isset( $data['coupon'] ) ) { 
  193. throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'coupon' ), 400 ); 
  194.  
  195. $data = $data['coupon']; 
  196.  
  197. // Check user permission 
  198. if ( ! current_user_can( 'publish_shop_coupons' ) ) { 
  199. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_coupon', __( 'You do not have permission to create coupons', 'woocommerce' ), 401 ); 
  200.  
  201. $data = apply_filters( 'woocommerce_api_create_coupon_data', $data, $this ); 
  202.  
  203. // Check if coupon code is specified 
  204. if ( ! isset( $data['code'] ) ) { 
  205. throw new WC_API_Exception( 'woocommerce_api_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ), 400 ); 
  206.  
  207. $coupon_code = wc_format_coupon_code( $data['code'] ); 
  208. $id_from_code = wc_get_coupon_id_by_code( $coupon_code ); 
  209.  
  210. if ( $id_from_code ) { 
  211. throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 ); 
  212.  
  213. $defaults = array( 
  214. 'type' => 'fixed_cart',  
  215. 'amount' => 0,  
  216. 'individual_use' => false,  
  217. 'product_ids' => array(),  
  218. 'exclude_product_ids' => array(),  
  219. 'usage_limit' => '',  
  220. 'usage_limit_per_user' => '',  
  221. 'limit_usage_to_x_items' => '',  
  222. 'usage_count' => '',  
  223. 'expiry_date' => '',  
  224. 'enable_free_shipping' => false,  
  225. 'product_category_ids' => array(),  
  226. 'exclude_product_category_ids' => array(),  
  227. 'exclude_sale_items' => false,  
  228. 'minimum_amount' => '',  
  229. 'maximum_amount' => '',  
  230. 'customer_emails' => array(),  
  231. 'description' => '',  
  232. ); 
  233.  
  234. $coupon_data = wp_parse_args( $data, $defaults ); 
  235.  
  236. // Validate coupon types 
  237. if ( ! in_array( wc_clean( $coupon_data['type'] ), array_keys( wc_get_coupon_types() ) ) ) { 
  238. 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 ); 
  239.  
  240. $new_coupon = array( 
  241. 'post_title' => $coupon_code,  
  242. 'post_content' => '',  
  243. 'post_status' => 'publish',  
  244. 'post_author' => get_current_user_id(),  
  245. 'post_type' => 'shop_coupon',  
  246. 'post_excerpt' => $coupon_data['description'],  
  247. ); 
  248.  
  249. $id = wp_insert_post( $new_coupon, true ); 
  250.  
  251. if ( is_wp_error( $id ) ) { 
  252. throw new WC_API_Exception( 'woocommerce_api_cannot_create_coupon', $id->get_error_message(), 400 ); 
  253.  
  254. // Set coupon meta 
  255. update_post_meta( $id, 'discount_type', $coupon_data['type'] ); 
  256. update_post_meta( $id, 'coupon_amount', wc_format_decimal( $coupon_data['amount'] ) ); 
  257. update_post_meta( $id, 'individual_use', ( true === $coupon_data['individual_use'] ) ? 'yes' : 'no' ); 
  258. update_post_meta( $id, 'product_ids', implode( ', ', array_filter( array_map( 'intval', $coupon_data['product_ids'] ) ) ) ); 
  259. update_post_meta( $id, 'exclude_product_ids', implode( ', ', array_filter( array_map( 'intval', $coupon_data['exclude_product_ids'] ) ) ) ); 
  260. update_post_meta( $id, 'usage_limit', absint( $coupon_data['usage_limit'] ) ); 
  261. update_post_meta( $id, 'usage_limit_per_user', absint( $coupon_data['usage_limit_per_user'] ) ); 
  262. update_post_meta( $id, 'limit_usage_to_x_items', absint( $coupon_data['limit_usage_to_x_items'] ) ); 
  263. update_post_meta( $id, 'usage_count', absint( $coupon_data['usage_count'] ) ); 
  264. update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ) ) ); 
  265. update_post_meta( $id, 'date_expires', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ), true ) ); 
  266. update_post_meta( $id, 'free_shipping', ( true === $coupon_data['enable_free_shipping'] ) ? 'yes' : 'no' ); 
  267. update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $coupon_data['product_category_ids'] ) ) ); 
  268. update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $coupon_data['exclude_product_category_ids'] ) ) ); 
  269. update_post_meta( $id, 'exclude_sale_items', ( true === $coupon_data['exclude_sale_items'] ) ? 'yes' : 'no' ); 
  270. update_post_meta( $id, 'minimum_amount', wc_format_decimal( $coupon_data['minimum_amount'] ) ); 
  271. update_post_meta( $id, 'maximum_amount', wc_format_decimal( $coupon_data['maximum_amount'] ) ); 
  272. update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $coupon_data['customer_emails'] ) ) ); 
  273.  
  274. do_action( 'woocommerce_api_create_coupon', $id, $data ); 
  275.  
  276. $this->server->send_status( 201 ); 
  277.  
  278. return $this->get_coupon( $id ); 
  279. } catch ( WC_API_Exception $e ) { 
  280. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  281.  
  282. /** 
  283. * Edit a coupon 
  284. * 
  285. * @since 2.2 
  286. * @param int $id the coupon ID 
  287. * @param array $data 
  288. * @return array 
  289. */ 
  290. public function edit_coupon( $id, $data ) { 
  291.  
  292. try { 
  293. if ( ! isset( $data['coupon'] ) ) { 
  294. throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'coupon' ), 400 ); 
  295.  
  296. $data = $data['coupon']; 
  297.  
  298. $id = $this->validate_request( $id, 'shop_coupon', 'edit' ); 
  299.  
  300. if ( is_wp_error( $id ) ) { 
  301. return $id; 
  302.  
  303. $data = apply_filters( 'woocommerce_api_edit_coupon_data', $data, $id, $this ); 
  304.  
  305. if ( isset( $data['code'] ) ) { 
  306. global $wpdb; 
  307.  
  308. $coupon_code = wc_format_coupon_code( $data['code'] ); 
  309. $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id ); 
  310.  
  311. if ( $id_from_code ) { 
  312. throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 ); 
  313.  
  314. $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_title' => $coupon_code ) ); 
  315.  
  316. if ( 0 === $updated ) { 
  317. throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 ); 
  318.  
  319. if ( isset( $data['description'] ) ) { 
  320. $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_excerpt' => $data['description'] ) ); 
  321.  
  322. if ( 0 === $updated ) { 
  323. throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 ); 
  324.  
  325. if ( isset( $data['type'] ) ) { 
  326. // Validate coupon types 
  327. if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_coupon_types() ) ) ) { 
  328. 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 ); 
  329. update_post_meta( $id, 'discount_type', $data['type'] ); 
  330.  
  331. if ( isset( $data['amount'] ) ) { 
  332. update_post_meta( $id, 'coupon_amount', wc_format_decimal( $data['amount'] ) ); 
  333.  
  334. if ( isset( $data['individual_use'] ) ) { 
  335. update_post_meta( $id, 'individual_use', ( true === $data['individual_use'] ) ? 'yes' : 'no' ); 
  336.  
  337. if ( isset( $data['product_ids'] ) ) { 
  338. update_post_meta( $id, 'product_ids', implode( ', ', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) ); 
  339.  
  340. if ( isset( $data['exclude_product_ids'] ) ) { 
  341. update_post_meta( $id, 'exclude_product_ids', implode( ', ', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) ); 
  342.  
  343. if ( isset( $data['usage_limit'] ) ) { 
  344. update_post_meta( $id, 'usage_limit', absint( $data['usage_limit'] ) ); 
  345.  
  346. if ( isset( $data['usage_limit_per_user'] ) ) { 
  347. update_post_meta( $id, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) ); 
  348.  
  349. if ( isset( $data['limit_usage_to_x_items'] ) ) { 
  350. update_post_meta( $id, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) ); 
  351.  
  352. if ( isset( $data['usage_count'] ) ) { 
  353. update_post_meta( $id, 'usage_count', absint( $data['usage_count'] ) ); 
  354.  
  355. if ( isset( $data['expiry_date'] ) ) { 
  356. update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) ); 
  357. update_post_meta( $id, 'date_expires', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ), true ) ); 
  358.  
  359. if ( isset( $data['enable_free_shipping'] ) ) { 
  360. update_post_meta( $id, 'free_shipping', ( true === $data['enable_free_shipping'] ) ? 'yes' : 'no' ); 
  361.  
  362. if ( isset( $data['product_category_ids'] ) ) { 
  363. update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $data['product_category_ids'] ) ) ); 
  364.  
  365. if ( isset( $data['exclude_product_category_ids'] ) ) { 
  366. update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $data['exclude_product_category_ids'] ) ) ); 
  367.  
  368. if ( isset( $data['exclude_sale_items'] ) ) { 
  369. update_post_meta( $id, 'exclude_sale_items', ( true === $data['exclude_sale_items'] ) ? 'yes' : 'no' ); 
  370.  
  371. if ( isset( $data['minimum_amount'] ) ) { 
  372. update_post_meta( $id, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) ); 
  373.  
  374. if ( isset( $data['maximum_amount'] ) ) { 
  375. update_post_meta( $id, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) ); 
  376.  
  377. if ( isset( $data['customer_emails'] ) ) { 
  378. update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $data['customer_emails'] ) ) ); 
  379.  
  380. do_action( 'woocommerce_api_edit_coupon', $id, $data ); 
  381.  
  382. return $this->get_coupon( $id ); 
  383. } catch ( WC_API_Exception $e ) { 
  384. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  385.  
  386. /** 
  387. * Delete a coupon 
  388. * 
  389. * @since 2.2 
  390. * @param int $id the coupon ID 
  391. * @param bool $force true to permanently delete coupon, false to move to trash 
  392. * @return array 
  393. */ 
  394. public function delete_coupon( $id, $force = false ) { 
  395.  
  396. $id = $this->validate_request( $id, 'shop_coupon', 'delete' ); 
  397.  
  398. if ( is_wp_error( $id ) ) { 
  399. return $id; 
  400.  
  401. do_action( 'woocommerce_api_delete_coupon', $id, $this ); 
  402.  
  403. return $this->delete( $id, 'shop_coupon', ( 'true' === $force ) ); 
  404.  
  405. /** 
  406. * expiry_date format 
  407. * 
  408. * @since 2.3.0 
  409. * @param string $expiry_date 
  410. * @param bool $as_timestamp (default: false) 
  411. * @return string|int 
  412. */ 
  413. protected function get_coupon_expiry_date( $expiry_date, $as_timestamp = false ) { 
  414. if ( '' != $expiry_date ) { 
  415. if ( $as_timestamp ) { 
  416. return strtotime( $expiry_date ); 
  417.  
  418. return date( 'Y-m-d', strtotime( $expiry_date ) ); 
  419.  
  420. return ''; 
  421.  
  422. /** 
  423. * Helper method to get coupon post objects 
  424. * 
  425. * @since 2.1 
  426. * @param array $args request arguments for filtering query 
  427. * @return WP_Query 
  428. */ 
  429. private function query_coupons( $args ) { 
  430.  
  431. // set base query arguments 
  432. $query_args = array( 
  433. 'fields' => 'ids',  
  434. 'post_type' => 'shop_coupon',  
  435. 'post_status' => 'publish',  
  436. ); 
  437.  
  438. $query_args = $this->merge_query_args( $query_args, $args ); 
  439.  
  440. return new WP_Query( $query_args ); 
  441.  
  442. /** 
  443. * Bulk update or insert coupons 
  444. * Accepts an array with coupons in the formats supported by 
  445. * WC_API_Coupons->create_coupon() and WC_API_Coupons->edit_coupon() 
  446. * 
  447. * @since 2.4.0 
  448. * @param array $data 
  449. * @return array 
  450. */ 
  451. public function bulk( $data ) { 
  452.  
  453. try { 
  454. if ( ! isset( $data['coupons'] ) ) { 
  455. throw new WC_API_Exception( 'woocommerce_api_missing_coupons_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'coupons' ), 400 ); 
  456.  
  457. $data = $data['coupons']; 
  458. $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'coupons' ); 
  459.  
  460. // Limit bulk operation 
  461. if ( count( $data ) > $limit ) { 
  462. 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 ); 
  463.  
  464. $coupons = array(); 
  465.  
  466. foreach ( $data as $_coupon ) { 
  467. $coupon_id = 0; 
  468.  
  469. // Try to get the coupon ID 
  470. if ( isset( $_coupon['id'] ) ) { 
  471. $coupon_id = intval( $_coupon['id'] ); 
  472.  
  473. if ( $coupon_id ) { 
  474.  
  475. // Coupon exists / edit coupon 
  476. $edit = $this->edit_coupon( $coupon_id, array( 'coupon' => $_coupon ) ); 
  477.  
  478. if ( is_wp_error( $edit ) ) { 
  479. $coupons[] = array( 
  480. 'id' => $coupon_id,  
  481. 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),  
  482. ); 
  483. } else { 
  484. $coupons[] = $edit['coupon']; 
  485. } else { 
  486.  
  487. // Coupon don't exists / create coupon 
  488. $new = $this->create_coupon( array( 'coupon' => $_coupon ) ); 
  489.  
  490. if ( is_wp_error( $new ) ) { 
  491. $coupons[] = array( 
  492. 'id' => $coupon_id,  
  493. 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),  
  494. ); 
  495. } else { 
  496. $coupons[] = $new['coupon']; 
  497.  
  498. return array( 'coupons' => apply_filters( 'woocommerce_api_coupons_bulk_response', $coupons, $this ) ); 
  499. } catch ( WC_API_Exception $e ) { 
  500. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
.