WC_REST_Legacy_Orders_Controller

REST API Legacy Orders controller class.

Defined (1)

The class is defined in the following location(s).

/includes/api/legacy/class-wc-rest-legacy-orders-controller.php  
  1. class WC_REST_Legacy_Orders_Controller extends WC_REST_CRUD_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v2'; 
  8.  
  9. /** 
  10. * Query args. 
  11. * @deprecated 3.0 
  12. * @param array $args 
  13. * @param WP_REST_Request $request 
  14. * @return array 
  15. */ 
  16. public function query_args( $args, $request ) { 
  17. global $wpdb; 
  18.  
  19. // Set post_status. 
  20. if ( 'any' !== $request['status'] ) { 
  21. $args['post_status'] = 'wc-' . $request['status']; 
  22. } else { 
  23. $args['post_status'] = 'any'; 
  24.  
  25. if ( ! empty( $request['customer'] ) ) { 
  26. if ( ! empty( $args['meta_query'] ) ) { 
  27. $args['meta_query'] = array(); 
  28.  
  29. $args['meta_query'][] = array( 
  30. 'key' => '_customer_user',  
  31. 'value' => $request['customer'],  
  32. 'type' => 'NUMERIC',  
  33. ); 
  34.  
  35. // Search by product. 
  36. if ( ! empty( $request['product'] ) ) { 
  37. $order_ids = $wpdb->get_col( $wpdb->prepare( " 
  38. SELECT order_id 
  39. FROM {$wpdb->prefix}woocommerce_order_items 
  40. WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d ) 
  41. AND order_item_type = 'line_item' 
  42. ", $request['product'] ) ); 
  43.  
  44. // Force WP_Query return empty if don't found any order. 
  45. $order_ids = ! empty( $order_ids ) ? $order_ids : array( 0 ); 
  46.  
  47. $args['post__in'] = $order_ids; 
  48.  
  49. // Search. 
  50. if ( ! empty( $args['s'] ) ) { 
  51. $order_ids = wc_order_search( $args['s'] ); 
  52.  
  53. if ( ! empty( $order_ids ) ) { 
  54. unset( $args['s'] ); 
  55. $args['post__in'] = array_merge( $order_ids, array( 0 ) ); 
  56.  
  57. return $args; 
  58.  
  59. /** 
  60. * Prepare a single order output for response. 
  61. * @deprecated 3.0 
  62. * @param WP_Post $post Post object. 
  63. * @param WP_REST_Request $request Request object. 
  64. * @return WP_REST_Response $data 
  65. */ 
  66. public function prepare_item_for_response( $post, $request ) { 
  67. $this->request = $request; 
  68. $statuses = wc_get_order_statuses(); 
  69. $order = wc_get_order( $post ); 
  70. $data = array_merge( array( 'id' => $order->get_id() ), $order->get_data() ); 
  71. $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); 
  72. $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); 
  73. $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); 
  74.  
  75. // Format decimal values. 
  76. foreach ( $format_decimal as $key ) { 
  77. $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); 
  78.  
  79. // Format date values. 
  80. foreach ( $format_date as $key ) { 
  81. $data[ $key ] = $data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $data[ $key ] ) ) ) : false; 
  82.  
  83. // Format the order status. 
  84. $data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status']; 
  85.  
  86. // Format line items. 
  87. foreach ( $format_line_items as $key ) { 
  88. $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) ); 
  89.  
  90. // Refunds. 
  91. $data['refunds'] = array(); 
  92. foreach ( $order->get_refunds() as $refund ) { 
  93. $data['refunds'][] = array( 
  94. 'id' => $refund->get_id(),  
  95. 'refund' => $refund->get_reason() ? $refund->get_reason() : '',  
  96. 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ),  
  97. ); 
  98.  
  99. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  100. $data = $this->add_additional_fields_to_object( $data, $request ); 
  101. $data = $this->filter_response_by_context( $data, $context ); 
  102. $response = rest_ensure_response( $data ); 
  103. $response->add_links( $this->prepare_links( $order, $request ) ); 
  104.  
  105. /** 
  106. * Filter the data for a response. 
  107. * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being 
  108. * prepared for the response. 
  109. * @param WP_REST_Response $response The response object. 
  110. * @param WP_Post $post Post object. 
  111. * @param WP_REST_Request $request Request object. 
  112. */ 
  113. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request ); 
  114.  
  115. /** 
  116. * Prepare a single order for create. 
  117. * @deprecated 3.0 
  118. * @param WP_REST_Request $request Request object. 
  119. * @return WP_Error|WC_Order $data Object. 
  120. */ 
  121. protected function prepare_item_for_database( $request ) { 
  122. $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; 
  123. $order = new WC_Order( $id ); 
  124. $schema = $this->get_item_schema(); 
  125. $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); 
  126.  
  127. // Handle all writable props 
  128. foreach ( $data_keys as $key ) { 
  129. $value = $request[ $key ]; 
  130.  
  131. if ( ! is_null( $value ) ) { 
  132. switch ( $key ) { 
  133. case 'billing' : 
  134. case 'shipping' : 
  135. $this->update_address( $order, $value, $key ); 
  136. break; 
  137. case 'line_items' : 
  138. case 'shipping_lines' : 
  139. case 'fee_lines' : 
  140. case 'coupon_lines' : 
  141. if ( is_array( $value ) ) { 
  142. foreach ( $value as $item ) { 
  143. if ( is_array( $item ) ) { 
  144. if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) { 
  145. $order->remove_item( $item['id'] ); 
  146. } else { 
  147. $this->set_item( $order, $key, $item ); 
  148. break; 
  149. case 'meta_data' : 
  150. if ( is_array( $value ) ) { 
  151. foreach ( $value as $meta ) { 
  152. $order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); 
  153. break; 
  154. default : 
  155. if ( is_callable( array( $order, "set_{$key}" ) ) ) { 
  156. $order->{"set_{$key}"}( $value ); 
  157. break; 
  158.  
  159. /** 
  160. * Filter the data for the insert. 
  161. * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being 
  162. * prepared for the response. 
  163. * @param WC_Order $order The prder object. 
  164. * @param WP_REST_Request $request Request object. 
  165. */ 
  166. return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $order, $request ); 
  167.  
  168. /** 
  169. * Create base WC Order object. 
  170. * @deprecated 3.0.0 
  171. * @param array $data 
  172. * @return WC_Order 
  173. */ 
  174. protected function create_base_order( $data ) { 
  175. return wc_create_order( $data ); 
  176.  
  177. /** 
  178. * Create order. 
  179. * @deprecated 3.0.0 
  180. * @param WP_REST_Request $request Full details about the request. 
  181. * @return int|WP_Error 
  182. */ 
  183. protected function create_order( $request ) { 
  184. try { 
  185. // Make sure customer exists. 
  186. if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] && false === get_user_by( 'id', $request['customer_id'] ) ) { 
  187. throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 ); 
  188.  
  189. $order = $this->prepare_item_for_database( $request ); 
  190. $order->set_created_via( 'rest-api' ); 
  191. $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); 
  192. $order->calculate_totals(); 
  193. $order->save(); 
  194.  
  195. // Handle set paid. 
  196. if ( true === $request['set_paid'] ) { 
  197. $order->payment_complete( $request['transaction_id'] ); 
  198.  
  199. return $order->get_id(); 
  200. } catch ( WC_Data_Exception $e ) { 
  201. return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); 
  202. } catch ( WC_REST_Exception $e ) { 
  203. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  204.  
  205. /** 
  206. * Update order. 
  207. * @deprecated 3.0.0 
  208. * @param WP_REST_Request $request Full details about the request. 
  209. * @return int|WP_Error 
  210. */ 
  211. protected function update_order( $request ) { 
  212. try { 
  213. $order = $this->prepare_item_for_database( $request ); 
  214. $order->save(); 
  215.  
  216. // Handle set paid. 
  217. if ( $order->needs_payment() && true === $request['set_paid'] ) { 
  218. $order->payment_complete( $request['transaction_id'] ); 
  219.  
  220. // If items have changed, recalculate order totals. 
  221. if ( isset( $request['billing'] ) || isset( $request['shipping'] ) || isset( $request['line_items'] ) || isset( $request['shipping_lines'] ) || isset( $request['fee_lines'] ) || isset( $request['coupon_lines'] ) ) { 
  222. $order->calculate_totals(); 
  223.  
  224. return $order->get_id(); 
  225. } catch ( WC_Data_Exception $e ) { 
  226. return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); 
  227. } catch ( WC_REST_Exception $e ) { 
  228. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );