class-wc-api-coupons.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * @version 2.1
  12. */
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit; // Exit if accessed directly
  15. }
  16. class WC_API_Coupons extends WC_API_Resource {
  17. /** @var string $base the route base */
  18. protected $base = '/coupons';
  19. /**
  20. * Register the routes for this class
  21. *
  22. * GET /coupons
  23. * GET /coupons/count
  24. * GET /coupons/<id>
  25. *
  26. * @since 2.1
  27. * @param array $routes
  28. * @return array
  29. */
  30. public function register_routes( $routes ) {
  31. # GET /coupons
  32. $routes[ $this->base ] = array(
  33. array( array( $this, 'get_coupons' ), WC_API_Server::READABLE ),
  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 /coupons/<id>
  40. $routes[ $this->base . '/(?P<id>\d+)' ] = array(
  41. array( array( $this, 'get_coupon' ), WC_API_Server::READABLE ),
  42. );
  43. # GET /coupons/code/<code>, note that coupon codes can contain spaces, dashes and underscores
  44. $routes[ $this->base . '/code/(?P<code>\w[\w\s\-]*)' ] = array(
  45. array( array( $this, 'get_coupon_by_code' ), WC_API_Server::READABLE ),
  46. );
  47. return $routes;
  48. }
  49. /**
  50. * Get all coupons
  51. *
  52. * @since 2.1
  53. * @param string $fields
  54. * @param array $filter
  55. * @param int $page
  56. * @return array
  57. */
  58. public function get_coupons( $fields = null, $filter = array(), $page = 1 ) {
  59. $filter['page'] = $page;
  60. $query = $this->query_coupons( $filter );
  61. $coupons = array();
  62. foreach ( $query->posts as $coupon_id ) {
  63. if ( ! $this->is_readable( $coupon_id ) ) {
  64. continue;
  65. }
  66. $coupons[] = current( $this->get_coupon( $coupon_id, $fields ) );
  67. }
  68. $this->server->add_pagination_headers( $query );
  69. return array( 'coupons' => $coupons );
  70. }
  71. /**
  72. * Get the coupon for the given ID
  73. *
  74. * @since 2.1
  75. *
  76. * @param int $id the coupon ID
  77. * @param string $fields fields to include in response
  78. *
  79. * @return array|WP_Error
  80. * @throws WC_API_Exception
  81. */
  82. public function get_coupon( $id, $fields = null ) {
  83. $id = $this->validate_request( $id, 'shop_coupon', 'read' );
  84. if ( is_wp_error( $id ) ) {
  85. return $id;
  86. }
  87. $coupon = new WC_Coupon( $id );
  88. if ( 0 === $coupon->get_id() ) {
  89. throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), 404 );
  90. }
  91. $coupon_data = array(
  92. 'id' => $coupon->get_id(),
  93. 'code' => $coupon->get_code(),
  94. 'type' => $coupon->get_discount_type(),
  95. 'created_at' => $this->server->format_datetime( $coupon->get_date_created() ? $coupon->get_date_created()->getTimestamp() : 0 ), // API gives UTC times.
  96. 'updated_at' => $this->server->format_datetime( $coupon->get_date_modified() ? $coupon->get_date_modified()->getTimestamp() : 0 ), // API gives UTC times.
  97. 'amount' => wc_format_decimal( $coupon->get_amount(), 2 ),
  98. 'individual_use' => $coupon->get_individual_use(),
  99. 'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
  100. 'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
  101. 'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
  102. 'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
  103. 'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
  104. 'usage_count' => (int) $coupon->get_usage_count(),
  105. 'expiry_date' => $this->server->format_datetime( $coupon->get_date_expires() ? $coupon->get_date_expires()->getTimestamp() : 0 ), // API gives UTC times.
  106. 'enable_free_shipping' => $coupon->get_free_shipping(),
  107. 'product_category_ids' => array_map( 'absint', (array) $coupon->get_product_categories() ),
  108. 'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),
  109. 'exclude_sale_items' => $coupon->get_exclude_sale_items(),
  110. 'minimum_amount' => wc_format_decimal( $coupon->get_minimum_amount(), 2 ),
  111. 'customer_emails' => $coupon->get_email_restrictions(),
  112. );
  113. return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) );
  114. }
  115. /**
  116. * Get the total number of coupons
  117. *
  118. * @since 2.1
  119. *
  120. * @param array $filter
  121. *
  122. * @return array|WP_Error
  123. */
  124. public function get_coupons_count( $filter = array() ) {
  125. $query = $this->query_coupons( $filter );
  126. if ( ! current_user_can( 'read_private_shop_coupons' ) ) {
  127. return new WP_Error( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), array( 'status' => 401 ) );
  128. }
  129. return array( 'count' => (int) $query->found_posts );
  130. }
  131. /**
  132. * Get the coupon for the given code
  133. *
  134. * @since 2.1
  135. * @param string $code the coupon code
  136. * @param string $fields fields to include in response
  137. * @return int|WP_Error
  138. */
  139. public function get_coupon_by_code( $code, $fields = null ) {
  140. global $wpdb;
  141. $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 ) );
  142. if ( is_null( $id ) ) {
  143. return new WP_Error( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), array( 'status' => 404 ) );
  144. }
  145. return $this->get_coupon( $id, $fields );
  146. }
  147. /**
  148. * Create a coupon
  149. *
  150. * @param array $data
  151. * @return array
  152. */
  153. public function create_coupon( $data ) {
  154. return array();
  155. }
  156. /**
  157. * Edit a coupon
  158. *
  159. * @param int $id the coupon ID
  160. * @param array $data
  161. * @return array|WP_Error
  162. */
  163. public function edit_coupon( $id, $data ) {
  164. $id = $this->validate_request( $id, 'shop_coupon', 'edit' );
  165. if ( is_wp_error( $id ) ) {
  166. return $id;
  167. }
  168. return $this->get_coupon( $id );
  169. }
  170. /**
  171. * Delete a coupon
  172. *
  173. * @param int $id the coupon ID
  174. * @param bool $force true to permanently delete coupon, false to move to trash
  175. * @return array|WP_Error
  176. */
  177. public function delete_coupon( $id, $force = false ) {
  178. $id = $this->validate_request( $id, 'shop_coupon', 'delete' );
  179. if ( is_wp_error( $id ) ) {
  180. return $id;
  181. }
  182. return $this->delete( $id, 'shop_coupon', ( 'true' === $force ) );
  183. }
  184. /**
  185. * Helper method to get coupon post objects
  186. *
  187. * @since 2.1
  188. * @param array $args request arguments for filtering query
  189. * @return WP_Query
  190. */
  191. private function query_coupons( $args ) {
  192. // set base query arguments
  193. $query_args = array(
  194. 'fields' => 'ids',
  195. 'post_type' => 'shop_coupon',
  196. 'post_status' => 'publish',
  197. );
  198. $query_args = $this->merge_query_args( $query_args, $args );
  199. return new WP_Query( $query_args );
  200. }
  201. }