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