class-wc-api-coupons.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. *
  129. * @param array $filter
  130. *
  131. * @return array|WP_Error
  132. */
  133. public function get_coupons_count( $filter = array() ) {
  134. try {
  135. if ( ! current_user_can( 'read_private_shop_coupons' ) ) {
  136. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), 401 );
  137. }
  138. $query = $this->query_coupons( $filter );
  139. return array( 'count' => (int) $query->found_posts );
  140. } catch ( WC_API_Exception $e ) {
  141. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  142. }
  143. }
  144. /**
  145. * Get the coupon for the given code
  146. *
  147. * @since 2.1
  148. * @param string $code the coupon code
  149. * @param string $fields fields to include in response
  150. * @return int|WP_Error
  151. */
  152. public function get_coupon_by_code( $code, $fields = null ) {
  153. global $wpdb;
  154. try {
  155. $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 ) );
  156. if ( is_null( $id ) ) {
  157. throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), 404 );
  158. }
  159. return $this->get_coupon( $id, $fields );
  160. } catch ( WC_API_Exception $e ) {
  161. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  162. }
  163. }
  164. /**
  165. * Create a coupon
  166. *
  167. * @since 2.2
  168. *
  169. * @param array $data
  170. *
  171. * @return array|WP_Error
  172. */
  173. public function create_coupon( $data ) {
  174. global $wpdb;
  175. try {
  176. if ( ! isset( $data['coupon'] ) ) {
  177. throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'coupon' ), 400 );
  178. }
  179. $data = $data['coupon'];
  180. // Check user permission
  181. if ( ! current_user_can( 'publish_shop_coupons' ) ) {
  182. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_coupon', __( 'You do not have permission to create coupons', 'woocommerce' ), 401 );
  183. }
  184. $data = apply_filters( 'woocommerce_api_create_coupon_data', $data, $this );
  185. // Check if coupon code is specified
  186. if ( ! isset( $data['code'] ) ) {
  187. throw new WC_API_Exception( 'woocommerce_api_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ), 400 );
  188. }
  189. $coupon_code = wc_format_coupon_code( $data['code'] );
  190. $id_from_code = wc_get_coupon_id_by_code( $coupon_code );
  191. if ( $id_from_code ) {
  192. throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
  193. }
  194. $defaults = array(
  195. 'type' => 'fixed_cart',
  196. 'amount' => 0,
  197. 'individual_use' => false,
  198. 'product_ids' => array(),
  199. 'exclude_product_ids' => array(),
  200. 'usage_limit' => '',
  201. 'usage_limit_per_user' => '',
  202. 'limit_usage_to_x_items' => '',
  203. 'usage_count' => '',
  204. 'expiry_date' => '',
  205. 'enable_free_shipping' => false,
  206. 'product_category_ids' => array(),
  207. 'exclude_product_category_ids' => array(),
  208. 'exclude_sale_items' => false,
  209. 'minimum_amount' => '',
  210. 'maximum_amount' => '',
  211. 'customer_emails' => array(),
  212. 'description' => '',
  213. );
  214. $coupon_data = wp_parse_args( $data, $defaults );
  215. // Validate coupon types
  216. if ( ! in_array( wc_clean( $coupon_data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
  217. 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 );
  218. }
  219. $new_coupon = array(
  220. 'post_title' => $coupon_code,
  221. 'post_content' => '',
  222. 'post_status' => 'publish',
  223. 'post_author' => get_current_user_id(),
  224. 'post_type' => 'shop_coupon',
  225. 'post_excerpt' => $coupon_data['description'],
  226. );
  227. $id = wp_insert_post( $new_coupon, true );
  228. if ( is_wp_error( $id ) ) {
  229. throw new WC_API_Exception( 'woocommerce_api_cannot_create_coupon', $id->get_error_message(), 400 );
  230. }
  231. // Set coupon meta
  232. update_post_meta( $id, 'discount_type', $coupon_data['type'] );
  233. update_post_meta( $id, 'coupon_amount', wc_format_decimal( $coupon_data['amount'] ) );
  234. update_post_meta( $id, 'individual_use', ( true === $coupon_data['individual_use'] ) ? 'yes' : 'no' );
  235. update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['product_ids'] ) ) ) );
  236. update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['exclude_product_ids'] ) ) ) );
  237. update_post_meta( $id, 'usage_limit', absint( $coupon_data['usage_limit'] ) );
  238. update_post_meta( $id, 'usage_limit_per_user', absint( $coupon_data['usage_limit_per_user'] ) );
  239. update_post_meta( $id, 'limit_usage_to_x_items', absint( $coupon_data['limit_usage_to_x_items'] ) );
  240. update_post_meta( $id, 'usage_count', absint( $coupon_data['usage_count'] ) );
  241. update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ) ) );
  242. update_post_meta( $id, 'date_expires', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ), true ) );
  243. update_post_meta( $id, 'free_shipping', ( true === $coupon_data['enable_free_shipping'] ) ? 'yes' : 'no' );
  244. update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $coupon_data['product_category_ids'] ) ) );
  245. update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $coupon_data['exclude_product_category_ids'] ) ) );
  246. update_post_meta( $id, 'exclude_sale_items', ( true === $coupon_data['exclude_sale_items'] ) ? 'yes' : 'no' );
  247. update_post_meta( $id, 'minimum_amount', wc_format_decimal( $coupon_data['minimum_amount'] ) );
  248. update_post_meta( $id, 'maximum_amount', wc_format_decimal( $coupon_data['maximum_amount'] ) );
  249. update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $coupon_data['customer_emails'] ) ) );
  250. do_action( 'woocommerce_api_create_coupon', $id, $data );
  251. do_action( 'woocommerce_new_coupon', $id );
  252. $this->server->send_status( 201 );
  253. return $this->get_coupon( $id );
  254. } catch ( WC_API_Exception $e ) {
  255. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  256. }
  257. }
  258. /**
  259. * Edit a coupon
  260. *
  261. * @since 2.2
  262. *
  263. * @param int $id the coupon ID
  264. * @param array $data
  265. *
  266. * @return array|WP_Error
  267. */
  268. public function edit_coupon( $id, $data ) {
  269. try {
  270. if ( ! isset( $data['coupon'] ) ) {
  271. throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'coupon' ), 400 );
  272. }
  273. $data = $data['coupon'];
  274. $id = $this->validate_request( $id, 'shop_coupon', 'edit' );
  275. if ( is_wp_error( $id ) ) {
  276. return $id;
  277. }
  278. $data = apply_filters( 'woocommerce_api_edit_coupon_data', $data, $id, $this );
  279. if ( isset( $data['code'] ) ) {
  280. global $wpdb;
  281. $coupon_code = wc_format_coupon_code( $data['code'] );
  282. $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
  283. if ( $id_from_code ) {
  284. throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
  285. }
  286. $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_title' => $coupon_code ) );
  287. if ( 0 === $updated ) {
  288. throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 );
  289. }
  290. }
  291. if ( isset( $data['description'] ) ) {
  292. $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_excerpt' => $data['description'] ) );
  293. if ( 0 === $updated ) {
  294. throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 );
  295. }
  296. }
  297. if ( isset( $data['type'] ) ) {
  298. // Validate coupon types
  299. if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
  300. 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 );
  301. }
  302. update_post_meta( $id, 'discount_type', $data['type'] );
  303. }
  304. if ( isset( $data['amount'] ) ) {
  305. update_post_meta( $id, 'coupon_amount', wc_format_decimal( $data['amount'] ) );
  306. }
  307. if ( isset( $data['individual_use'] ) ) {
  308. update_post_meta( $id, 'individual_use', ( true === $data['individual_use'] ) ? 'yes' : 'no' );
  309. }
  310. if ( isset( $data['product_ids'] ) ) {
  311. update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) );
  312. }
  313. if ( isset( $data['exclude_product_ids'] ) ) {
  314. update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) );
  315. }
  316. if ( isset( $data['usage_limit'] ) ) {
  317. update_post_meta( $id, 'usage_limit', absint( $data['usage_limit'] ) );
  318. }
  319. if ( isset( $data['usage_limit_per_user'] ) ) {
  320. update_post_meta( $id, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) );
  321. }
  322. if ( isset( $data['limit_usage_to_x_items'] ) ) {
  323. update_post_meta( $id, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) );
  324. }
  325. if ( isset( $data['usage_count'] ) ) {
  326. update_post_meta( $id, 'usage_count', absint( $data['usage_count'] ) );
  327. }
  328. if ( isset( $data['expiry_date'] ) ) {
  329. update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) );
  330. update_post_meta( $id, 'date_expires', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ), true ) );
  331. }
  332. if ( isset( $data['enable_free_shipping'] ) ) {
  333. update_post_meta( $id, 'free_shipping', ( true === $data['enable_free_shipping'] ) ? 'yes' : 'no' );
  334. }
  335. if ( isset( $data['product_category_ids'] ) ) {
  336. update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $data['product_category_ids'] ) ) );
  337. }
  338. if ( isset( $data['exclude_product_category_ids'] ) ) {
  339. update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $data['exclude_product_category_ids'] ) ) );
  340. }
  341. if ( isset( $data['exclude_sale_items'] ) ) {
  342. update_post_meta( $id, 'exclude_sale_items', ( true === $data['exclude_sale_items'] ) ? 'yes' : 'no' );
  343. }
  344. if ( isset( $data['minimum_amount'] ) ) {
  345. update_post_meta( $id, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) );
  346. }
  347. if ( isset( $data['maximum_amount'] ) ) {
  348. update_post_meta( $id, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) );
  349. }
  350. if ( isset( $data['customer_emails'] ) ) {
  351. update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $data['customer_emails'] ) ) );
  352. }
  353. do_action( 'woocommerce_api_edit_coupon', $id, $data );
  354. do_action( 'woocommerce_update_coupon', $id );
  355. return $this->get_coupon( $id );
  356. } catch ( WC_API_Exception $e ) {
  357. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  358. }
  359. }
  360. /**
  361. * Delete a coupon
  362. *
  363. * @since 2.2
  364. * @param int $id the coupon ID
  365. * @param bool $force true to permanently delete coupon, false to move to trash
  366. * @return array|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. // Coupon exists / edit coupon
  440. if ( $coupon_id ) {
  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. }