PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/api/v2/class-wc-api-coupons.php

https://gitlab.com/webkod3r/tripolis
PHP | 581 lines | 354 code | 109 blank | 118 comment | 50 complexity | 3469ff090b3016aa9715ff93446b3074 MD5 | raw file
  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. global $wpdb;
  87. try {
  88. $id = $this->validate_request( $id, 'shop_coupon', 'read' );
  89. if ( is_wp_error( $id ) ) {
  90. return $id;
  91. }
  92. // get the coupon code
  93. $code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $id ) );
  94. if ( is_null( $code ) ) {
  95. throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), 404 );
  96. }
  97. $coupon = new WC_Coupon( $code );
  98. $coupon_post = get_post( $coupon->id );
  99. $coupon_data = array(
  100. 'id' => $coupon->id,
  101. 'code' => $coupon->code,
  102. 'type' => $coupon->type,
  103. 'created_at' => $this->server->format_datetime( $coupon_post->post_date_gmt ),
  104. 'updated_at' => $this->server->format_datetime( $coupon_post->post_modified_gmt ),
  105. 'amount' => wc_format_decimal( $coupon->coupon_amount, 2 ),
  106. 'individual_use' => ( 'yes' === $coupon->individual_use ),
  107. 'product_ids' => array_map( 'absint', (array) $coupon->product_ids ),
  108. 'exclude_product_ids' => array_map( 'absint', (array) $coupon->exclude_product_ids ),
  109. 'usage_limit' => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null,
  110. 'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
  111. 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items,
  112. 'usage_count' => (int) $coupon->usage_count,
  113. 'expiry_date' => ( ! empty( $coupon->expiry_date ) ) ? $this->server->format_datetime( $coupon->expiry_date ) : null,
  114. 'enable_free_shipping' => $coupon->enable_free_shipping(),
  115. 'product_category_ids' => array_map( 'absint', (array) $coupon->product_categories ),
  116. 'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
  117. 'exclude_sale_items' => $coupon->exclude_sale_items(),
  118. 'minimum_amount' => wc_format_decimal( $coupon->minimum_amount, 2 ),
  119. 'maximum_amount' => wc_format_decimal( $coupon->maximum_amount, 2 ),
  120. 'customer_emails' => $coupon->customer_email,
  121. 'description' => $coupon_post->post_excerpt,
  122. );
  123. return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) );
  124. } catch ( WC_API_Exception $e ) {
  125. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  126. }
  127. }
  128. /**
  129. * Get the total number of coupons
  130. *
  131. * @since 2.1
  132. * @param array $filter
  133. * @return array
  134. */
  135. public function get_coupons_count( $filter = array() ) {
  136. try {
  137. if ( ! current_user_can( 'read_private_shop_coupons' ) ) {
  138. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), 401 );
  139. }
  140. $query = $this->query_coupons( $filter );
  141. return array( 'count' => (int) $query->found_posts );
  142. } catch ( WC_API_Exception $e ) {
  143. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  144. }
  145. }
  146. /**
  147. * Get the coupon for the given code
  148. *
  149. * @since 2.1
  150. * @param string $code the coupon code
  151. * @param string $fields fields to include in response
  152. * @return int|WP_Error
  153. */
  154. public function get_coupon_by_code( $code, $fields = null ) {
  155. global $wpdb;
  156. try {
  157. $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 ) );
  158. if ( is_null( $id ) ) {
  159. throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), 404 );
  160. }
  161. return $this->get_coupon( $id, $fields );
  162. } catch ( WC_API_Exception $e ) {
  163. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  164. }
  165. }
  166. /**
  167. * Create a coupon
  168. *
  169. * @since 2.2
  170. * @param array $data
  171. * @return array
  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 = apply_filters( 'woocommerce_coupon_code', $data['code'] );
  190. // Check for duplicate coupon codes
  191. $coupon_found = $wpdb->get_var( $wpdb->prepare( "
  192. SELECT $wpdb->posts.ID
  193. FROM $wpdb->posts
  194. WHERE $wpdb->posts.post_type = 'shop_coupon'
  195. AND $wpdb->posts.post_status = 'publish'
  196. AND $wpdb->posts.post_title = '%s'
  197. ", $coupon_code ) );
  198. if ( $coupon_found ) {
  199. throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
  200. }
  201. $defaults = array(
  202. 'type' => 'fixed_cart',
  203. 'amount' => 0,
  204. 'individual_use' => false,
  205. 'product_ids' => array(),
  206. 'exclude_product_ids' => array(),
  207. 'usage_limit' => '',
  208. 'usage_limit_per_user' => '',
  209. 'limit_usage_to_x_items' => '',
  210. 'usage_count' => '',
  211. 'expiry_date' => '',
  212. 'enable_free_shipping' => false,
  213. 'product_category_ids' => array(),
  214. 'exclude_product_category_ids' => array(),
  215. 'exclude_sale_items' => false,
  216. 'minimum_amount' => '',
  217. 'maximum_amount' => '',
  218. 'customer_emails' => array(),
  219. 'description' => ''
  220. );
  221. $coupon_data = wp_parse_args( $data, $defaults );
  222. // Validate coupon types
  223. if ( ! in_array( wc_clean( $coupon_data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
  224. 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 );
  225. }
  226. $new_coupon = array(
  227. 'post_title' => $coupon_code,
  228. 'post_content' => '',
  229. 'post_status' => 'publish',
  230. 'post_author' => get_current_user_id(),
  231. 'post_type' => 'shop_coupon',
  232. 'post_excerpt' => $coupon_data['description']
  233. );
  234. $id = wp_insert_post( $new_coupon, true );
  235. if ( is_wp_error( $id ) ) {
  236. throw new WC_API_Exception( 'woocommerce_api_cannot_create_coupon', $id->get_error_message(), 400 );
  237. }
  238. // Set coupon meta
  239. update_post_meta( $id, 'discount_type', $coupon_data['type'] );
  240. update_post_meta( $id, 'coupon_amount', wc_format_decimal( $coupon_data['amount'] ) );
  241. update_post_meta( $id, 'individual_use', ( true === $coupon_data['individual_use'] ) ? 'yes' : 'no' );
  242. update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['product_ids'] ) ) ) );
  243. update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $coupon_data['exclude_product_ids'] ) ) ) );
  244. update_post_meta( $id, 'usage_limit', absint( $coupon_data['usage_limit'] ) );
  245. update_post_meta( $id, 'usage_limit_per_user', absint( $coupon_data['usage_limit_per_user'] ) );
  246. update_post_meta( $id, 'limit_usage_to_x_items', absint( $coupon_data['limit_usage_to_x_items'] ) );
  247. update_post_meta( $id, 'usage_count', absint( $coupon_data['usage_count'] ) );
  248. update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $coupon_data['expiry_date'] ) ) );
  249. update_post_meta( $id, 'free_shipping', ( true === $coupon_data['enable_free_shipping'] ) ? 'yes' : 'no' );
  250. update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $coupon_data['product_category_ids'] ) ) );
  251. update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $coupon_data['exclude_product_category_ids'] ) ) );
  252. update_post_meta( $id, 'exclude_sale_items', ( true === $coupon_data['exclude_sale_items'] ) ? 'yes' : 'no' );
  253. update_post_meta( $id, 'minimum_amount', wc_format_decimal( $coupon_data['minimum_amount'] ) );
  254. update_post_meta( $id, 'maximum_amount', wc_format_decimal( $coupon_data['maximum_amount'] ) );
  255. update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $coupon_data['customer_emails'] ) ) );
  256. do_action( 'woocommerce_api_create_coupon', $id, $data );
  257. $this->server->send_status( 201 );
  258. return $this->get_coupon( $id );
  259. } catch ( WC_API_Exception $e ) {
  260. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  261. }
  262. }
  263. /**
  264. * Edit a coupon
  265. *
  266. * @since 2.2
  267. * @param int $id the coupon ID
  268. * @param array $data
  269. * @return array
  270. */
  271. public function edit_coupon( $id, $data ) {
  272. try {
  273. if ( ! isset( $data['coupon'] ) ) {
  274. throw new WC_API_Exception( 'woocommerce_api_missing_coupon_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'coupon' ), 400 );
  275. }
  276. $data = $data['coupon'];
  277. $id = $this->validate_request( $id, 'shop_coupon', 'edit' );
  278. if ( is_wp_error( $id ) ) {
  279. return $id;
  280. }
  281. $data = apply_filters( 'woocommerce_api_edit_coupon_data', $data, $id, $this );
  282. if ( isset( $data['code'] ) ) {
  283. global $wpdb;
  284. $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] );
  285. // Check for duplicate coupon codes
  286. $coupon_found = $wpdb->get_var( $wpdb->prepare( "
  287. SELECT $wpdb->posts.ID
  288. FROM $wpdb->posts
  289. WHERE $wpdb->posts.post_type = 'shop_coupon'
  290. AND $wpdb->posts.post_status = 'publish'
  291. AND $wpdb->posts.post_title = '%s'
  292. AND $wpdb->posts.ID != %s
  293. ", $coupon_code, $id ) );
  294. if ( $coupon_found ) {
  295. throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
  296. }
  297. $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_title' => $coupon_code ) );
  298. if ( 0 === $updated ) {
  299. throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 );
  300. }
  301. }
  302. if ( isset( $data['description'] ) ) {
  303. $updated = wp_update_post( array( 'ID' => intval( $id ), 'post_excerpt' => $data['description'] ) );
  304. if ( 0 === $updated ) {
  305. throw new WC_API_Exception( 'woocommerce_api_cannot_update_coupon', __( 'Failed to update coupon', 'woocommerce' ), 400 );
  306. }
  307. }
  308. if ( isset( $data['type'] ) ) {
  309. // Validate coupon types
  310. if ( ! in_array( wc_clean( $data['type'] ), array_keys( wc_get_coupon_types() ) ) ) {
  311. 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 );
  312. }
  313. update_post_meta( $id, 'discount_type', $data['type'] );
  314. }
  315. if ( isset( $data['amount'] ) ) {
  316. update_post_meta( $id, 'coupon_amount', wc_format_decimal( $data['amount'] ) );
  317. }
  318. if ( isset( $data['individual_use'] ) ) {
  319. update_post_meta( $id, 'individual_use', ( true === $data['individual_use'] ) ? 'yes' : 'no' );
  320. }
  321. if ( isset( $data['product_ids'] ) ) {
  322. update_post_meta( $id, 'product_ids', implode( ',', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) );
  323. }
  324. if ( isset( $data['exclude_product_ids'] ) ) {
  325. update_post_meta( $id, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) );
  326. }
  327. if ( isset( $data['usage_limit'] ) ) {
  328. update_post_meta( $id, 'usage_limit', absint( $data['usage_limit'] ) );
  329. }
  330. if ( isset( $data['usage_limit_per_user'] ) ) {
  331. update_post_meta( $id, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) );
  332. }
  333. if ( isset( $data['limit_usage_to_x_items'] ) ) {
  334. update_post_meta( $id, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) );
  335. }
  336. if ( isset( $data['usage_count'] ) ) {
  337. update_post_meta( $id, 'usage_count', absint( $data['usage_count'] ) );
  338. }
  339. if ( isset( $data['expiry_date'] ) ) {
  340. update_post_meta( $id, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) );
  341. }
  342. if ( isset( $data['enable_free_shipping'] ) ) {
  343. update_post_meta( $id, 'free_shipping', ( true === $data['enable_free_shipping'] ) ? 'yes' : 'no' );
  344. }
  345. if ( isset( $data['product_category_ids'] ) ) {
  346. update_post_meta( $id, 'product_categories', array_filter( array_map( 'intval', $data['product_category_ids'] ) ) );
  347. }
  348. if ( isset( $data['exclude_product_category_ids'] ) ) {
  349. update_post_meta( $id, 'exclude_product_categories', array_filter( array_map( 'intval', $data['exclude_product_category_ids'] ) ) );
  350. }
  351. if ( isset( $data['exclude_sale_items'] ) ) {
  352. update_post_meta( $id, 'exclude_sale_items', ( true === $data['exclude_sale_items'] ) ? 'yes' : 'no' );
  353. }
  354. if ( isset( $data['minimum_amount'] ) ) {
  355. update_post_meta( $id, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) );
  356. }
  357. if ( isset( $data['maximum_amount'] ) ) {
  358. update_post_meta( $id, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) );
  359. }
  360. if ( isset( $data['customer_emails'] ) ) {
  361. update_post_meta( $id, 'customer_email', array_filter( array_map( 'sanitize_email', $data['customer_emails'] ) ) );
  362. }
  363. do_action( 'woocommerce_api_edit_coupon', $id, $data );
  364. return $this->get_coupon( $id );
  365. } catch ( WC_API_Exception $e ) {
  366. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  367. }
  368. }
  369. /**
  370. * Delete a coupon
  371. *
  372. * @since 2.2
  373. * @param int $id the coupon ID
  374. * @param bool $force true to permanently delete coupon, false to move to trash
  375. * @return array
  376. */
  377. public function delete_coupon( $id, $force = false ) {
  378. $id = $this->validate_request( $id, 'shop_coupon', 'delete' );
  379. if ( is_wp_error( $id ) ) {
  380. return $id;
  381. }
  382. do_action( 'woocommerce_api_delete_coupon', $id, $this );
  383. return $this->delete( $id, 'shop_coupon', ( 'true' === $force ) );
  384. }
  385. /**
  386. * expiry_date format
  387. *
  388. * @since 2.3.0
  389. * @param string $expiry_date
  390. * @return string
  391. */
  392. protected function get_coupon_expiry_date( $expiry_date ) {
  393. if ( '' != $expiry_date ) {
  394. return date( 'Y-m-d', strtotime( $expiry_date ) );
  395. }
  396. return '';
  397. }
  398. /**
  399. * Helper method to get coupon post objects
  400. *
  401. * @since 2.1
  402. * @param array $args request arguments for filtering query
  403. * @return WP_Query
  404. */
  405. private function query_coupons( $args ) {
  406. // set base query arguments
  407. $query_args = array(
  408. 'fields' => 'ids',
  409. 'post_type' => 'shop_coupon',
  410. 'post_status' => 'publish',
  411. );
  412. $query_args = $this->merge_query_args( $query_args, $args );
  413. return new WP_Query( $query_args );
  414. }
  415. /**
  416. * Bulk update or insert coupons
  417. * Accepts an array with coupons in the formats supported by
  418. * WC_API_Coupons->create_coupon() and WC_API_Coupons->edit_coupon()
  419. *
  420. * @since 2.4.0
  421. * @param array $data
  422. * @return array
  423. */
  424. public function bulk( $data ) {
  425. try {
  426. if ( ! isset( $data['coupons'] ) ) {
  427. throw new WC_API_Exception( 'woocommerce_api_missing_coupons_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'coupons' ), 400 );
  428. }
  429. $data = $data['coupons'];
  430. $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'coupons' );
  431. // Limit bulk operation
  432. if ( count( $data ) > $limit ) {
  433. 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 );
  434. }
  435. $coupons = array();
  436. foreach ( $data as $_coupon ) {
  437. $coupon_id = 0;
  438. // Try to get the coupon ID
  439. if ( isset( $_coupon['id'] ) ) {
  440. $coupon_id = intval( $_coupon['id'] );
  441. }
  442. // Coupon exists / edit coupon
  443. if ( $coupon_id ) {
  444. $edit = $this->edit_coupon( $coupon_id, array( 'coupon' => $_coupon ) );
  445. if ( is_wp_error( $edit ) ) {
  446. $coupons[] = array(
  447. 'id' => $coupon_id,
  448. 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() )
  449. );
  450. } else {
  451. $coupons[] = $edit['coupon'];
  452. }
  453. }
  454. // Coupon don't exists / create coupon
  455. else {
  456. $new = $this->create_coupon( array( 'coupon' => $_coupon ) );
  457. if ( is_wp_error( $new ) ) {
  458. $coupons[] = array(
  459. 'id' => $coupon_id,
  460. 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() )
  461. );
  462. } else {
  463. $coupons[] = $new['coupon'];
  464. }
  465. }
  466. }
  467. return array( 'coupons' => apply_filters( 'woocommerce_api_coupons_bulk_response', $coupons, $this ) );
  468. } catch ( WC_API_Exception $e ) {
  469. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
  470. }
  471. }
  472. }