class-wc-api-coupons.php 20 KB

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