class-wc-rest-legacy-orders-controller.php 9.3 KB

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