WC_REST_Orders_Controller

REST API Orders controller class.

Defined (1)

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

/includes/api/class-wc-rest-orders-controller.php  
  1. class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v2'; 
  8.  
  9. /** 
  10. * Route base. 
  11. * @var string 
  12. */ 
  13. protected $rest_base = 'orders'; 
  14.  
  15. /** 
  16. * Post type. 
  17. * @var string 
  18. */ 
  19. protected $post_type = 'shop_order'; 
  20.  
  21. /** 
  22. * If object is hierarchical. 
  23. * @var bool 
  24. */ 
  25. protected $hierarchical = true; 
  26.  
  27. /** 
  28. * Stores the request. 
  29. * @var array 
  30. */ 
  31. protected $request = array(); 
  32.  
  33. /** 
  34. * Register the routes for orders. 
  35. */ 
  36. public function register_routes() { 
  37. register_rest_route( $this->namespace, '/' . $this->rest_base, array( 
  38. array( 
  39. 'methods' => WP_REST_Server::READABLE,  
  40. 'callback' => array( $this, 'get_items' ),  
  41. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  42. 'args' => $this->get_collection_params(),  
  43. ),  
  44. array( 
  45. 'methods' => WP_REST_Server::CREATABLE,  
  46. 'callback' => array( $this, 'create_item' ),  
  47. 'permission_callback' => array( $this, 'create_item_permissions_check' ),  
  48. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),  
  49. ),  
  50. 'schema' => array( $this, 'get_public_item_schema' ),  
  51. ) ); 
  52.  
  53. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 
  54. 'args' => array( 
  55. 'id' => array( 
  56. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  57. 'type' => 'integer',  
  58. ),  
  59. ),  
  60. array( 
  61. 'methods' => WP_REST_Server::READABLE,  
  62. 'callback' => array( $this, 'get_item' ),  
  63. 'permission_callback' => array( $this, 'get_item_permissions_check' ),  
  64. 'args' => array( 
  65. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  66. ),  
  67. ),  
  68. array( 
  69. 'methods' => WP_REST_Server::EDITABLE,  
  70. 'callback' => array( $this, 'update_item' ),  
  71. 'permission_callback' => array( $this, 'update_item_permissions_check' ),  
  72. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  73. ),  
  74. array( 
  75. 'methods' => WP_REST_Server::DELETABLE,  
  76. 'callback' => array( $this, 'delete_item' ),  
  77. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),  
  78. 'args' => array( 
  79. 'force' => array( 
  80. 'default' => false,  
  81. 'type' => 'boolean',  
  82. 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),  
  83. ),  
  84. ),  
  85. ),  
  86. 'schema' => array( $this, 'get_public_item_schema' ),  
  87. ) ); 
  88.  
  89. register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( 
  90. array( 
  91. 'methods' => WP_REST_Server::EDITABLE,  
  92. 'callback' => array( $this, 'batch_items' ),  
  93. 'permission_callback' => array( $this, 'batch_items_permissions_check' ),  
  94. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  95. ),  
  96. 'schema' => array( $this, 'get_public_batch_schema' ),  
  97. ) ); 
  98.  
  99. /** 
  100. * Get object. 
  101. * @since 3.0.0 
  102. * @param int $id Object ID. 
  103. * @return WC_Data 
  104. */ 
  105. protected function get_object( $id ) { 
  106. return wc_get_order( $id ); 
  107.  
  108. /** 
  109. * Expands an order item to get its data. 
  110. * @param WC_Order_item $item 
  111. * @return array 
  112. */ 
  113. protected function get_order_item_data( $item ) { 
  114. $data = $item->get_data(); 
  115. $format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' ); 
  116.  
  117. // Format decimal values. 
  118. foreach ( $format_decimal as $key ) { 
  119. if ( isset( $data[ $key ] ) ) { 
  120. $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); 
  121.  
  122. // Add SKU and PRICE to products. 
  123. if ( is_callable( array( $item, 'get_product' ) ) ) { 
  124. $data['sku'] = $item->get_product() ? $item->get_product()->get_sku(): null; 
  125. $data['price'] = $item->get_total() / max( 1, $item->get_quantity() ); 
  126.  
  127. // Format taxes. 
  128. if ( ! empty( $data['taxes']['total'] ) ) { 
  129. $taxes = array(); 
  130.  
  131. foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) { 
  132. $taxes[] = array( 
  133. 'id' => $tax_rate_id,  
  134. 'total' => $tax,  
  135. 'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '',  
  136. ); 
  137. $data['taxes'] = $taxes; 
  138. } elseif ( isset( $data['taxes'] ) ) { 
  139. $data['taxes'] = array(); 
  140.  
  141. // Remove names for coupons, taxes and shipping. 
  142. if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) { 
  143. unset( $data['name'] ); 
  144.  
  145. // Remove props we don't want to expose. 
  146. unset( $data['order_id'] ); 
  147. unset( $data['type'] ); 
  148.  
  149. return $data; 
  150.  
  151. /** 
  152. * Get formatted item data. 
  153. * @since 3.0.0 
  154. * @param WC_Data $object WC_Data instance. 
  155. * @return array 
  156. */ 
  157. protected function get_formatted_item_data( $object ) { 
  158. $data = $object->get_data(); 
  159. $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); 
  160. $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); 
  161. $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); 
  162.  
  163. // Format decimal values. 
  164. foreach ( $format_decimal as $key ) { 
  165. $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); 
  166.  
  167. // Format date values. 
  168. foreach ( $format_date as $key ) { 
  169. $datetime = $data[ $key ]; 
  170. $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); 
  171. $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); 
  172.  
  173. // Format the order status. 
  174. $data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status']; 
  175.  
  176. // Format line items. 
  177. foreach ( $format_line_items as $key ) { 
  178. $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) ); 
  179.  
  180. // Refunds. 
  181. $data['refunds'] = array(); 
  182. foreach ( $object->get_refunds() as $refund ) { 
  183. $data['refunds'][] = array( 
  184. 'id' => $refund->get_id(),  
  185. 'refund' => $refund->get_reason() ? $refund->get_reason() : '',  
  186. 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ),  
  187. ); 
  188.  
  189. return array( 
  190. 'id' => $object->get_id(),  
  191. 'parent_id' => $data['parent_id'],  
  192. 'number' => $data['number'],  
  193. 'order_key' => $data['order_key'],  
  194. 'created_via' => $data['created_via'],  
  195. 'version' => $data['version'],  
  196. 'status' => $data['status'],  
  197. 'currency' => $data['currency'],  
  198. 'date_created' => $data['date_created'],  
  199. 'date_created_gmt' => $data['date_created_gmt'],  
  200. 'date_modified' => $data['date_modified'],  
  201. 'date_modified_gmt' => $data['date_modified_gmt'],  
  202. 'discount_total' => $data['discount_total'],  
  203. 'discount_tax' => $data['discount_tax'],  
  204. 'shipping_total' => $data['shipping_total'],  
  205. 'shipping_tax' => $data['shipping_tax'],  
  206. 'cart_tax' => $data['cart_tax'],  
  207. 'total' => $data['total'],  
  208. 'total_tax' => $data['total_tax'],  
  209. 'prices_include_tax' => $data['prices_include_tax'],  
  210. 'customer_id' => $data['customer_id'],  
  211. 'customer_ip_address' => $data['customer_ip_address'],  
  212. 'customer_user_agent' => $data['customer_user_agent'],  
  213. 'customer_note' => $data['customer_note'],  
  214. 'billing' => $data['billing'],  
  215. 'shipping' => $data['shipping'],  
  216. 'payment_method' => $data['payment_method'],  
  217. 'payment_method_title' => $data['payment_method_title'],  
  218. 'transaction_id' => $data['transaction_id'],  
  219. 'date_paid' => $data['date_paid'],  
  220. 'date_paid_gmt' => $data['date_paid_gmt'],  
  221. 'date_completed' => $data['date_completed'],  
  222. 'date_completed_gmt' => $data['date_completed_gmt'],  
  223. 'cart_hash' => $data['cart_hash'],  
  224. 'meta_data' => $data['meta_data'],  
  225. 'line_items' => $data['line_items'],  
  226. 'tax_lines' => $data['tax_lines'],  
  227. 'shipping_lines' => $data['shipping_lines'],  
  228. 'fee_lines' => $data['fee_lines'],  
  229. 'coupon_lines' => $data['coupon_lines'],  
  230. 'refunds' => $data['refunds'],  
  231. ); 
  232.  
  233. /** 
  234. * Prepare a single order output for response. 
  235. * @since 3.0.0 
  236. * @param WC_Data $object Object data. 
  237. * @param WP_REST_Request $request Request object. 
  238. * @return WP_REST_Response 
  239. */ 
  240. public function prepare_object_for_response( $object, $request ) { 
  241. $this->request = $request; 
  242. $data = $this->get_formatted_item_data( $object ); 
  243. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  244. $data = $this->add_additional_fields_to_object( $data, $request ); 
  245. $data = $this->filter_response_by_context( $data, $context ); 
  246. $response = rest_ensure_response( $data ); 
  247. $response->add_links( $this->prepare_links( $object, $request ) ); 
  248.  
  249. /** 
  250. * Filter the data for a response. 
  251. * The dynamic portion of the hook name, $this->post_type,  
  252. * refers to object type being prepared for the response. 
  253. * @param WP_REST_Response $response The response object. 
  254. * @param WC_Data $object Object data. 
  255. * @param WP_REST_Request $request Request object. 
  256. */ 
  257. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request ); 
  258.  
  259. /** 
  260. * Prepare links for the request. 
  261. * @param WC_Data $object Object data. 
  262. * @param WP_REST_Request $request Request object. 
  263. * @return array Links for the given post. 
  264. */ 
  265. protected function prepare_links( $object, $request ) { 
  266. $links = array( 
  267. 'self' => array( 
  268. 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ),  
  269. ),  
  270. 'collection' => array( 
  271. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),  
  272. ),  
  273. ); 
  274.  
  275. if ( 0 !== (int) $object->get_customer_id() ) { 
  276. $links['customer'] = array( 
  277. 'href' => rest_url( sprintf( '/%s/customers/%d', $this->namespace, $object->get_customer_id() ) ),  
  278. ); 
  279.  
  280. if ( 0 !== (int) $object->get_parent_id() ) { 
  281. $links['up'] = array( 
  282. 'href' => rest_url( sprintf( '/%s/orders/%d', $this->namespace, $object->get_parent_id() ) ),  
  283. ); 
  284.  
  285. return $links; 
  286.  
  287. /** 
  288. * Prepare objects query. 
  289. * @since 3.0.0 
  290. * @param WP_REST_Request $request Full details about the request. 
  291. * @return array 
  292. */ 
  293. protected function prepare_objects_query( $request ) { 
  294. global $wpdb; 
  295.  
  296. $args = parent::prepare_objects_query( $request ); 
  297.  
  298. // Set post_status. 
  299. if ( 'any' !== $request['status'] ) { 
  300. $args['post_status'] = 'wc-' . $request['status']; 
  301. } else { 
  302. $args['post_status'] = 'any'; 
  303.  
  304. if ( isset( $request['customer'] ) ) { 
  305. if ( ! empty( $args['meta_query'] ) ) { 
  306. $args['meta_query'] = array(); 
  307.  
  308. $args['meta_query'][] = array( 
  309. 'key' => '_customer_user',  
  310. 'value' => $request['customer'],  
  311. 'type' => 'NUMERIC',  
  312. ); 
  313.  
  314. // Search by product. 
  315. if ( ! empty( $request['product'] ) ) { 
  316. $order_ids = $wpdb->get_col( $wpdb->prepare( " 
  317. SELECT order_id 
  318. FROM {$wpdb->prefix}woocommerce_order_items 
  319. WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d ) 
  320. AND order_item_type = 'line_item' 
  321. ", $request['product'] ) ); 
  322.  
  323. // Force WP_Query return empty if don't found any order. 
  324. $order_ids = ! empty( $order_ids ) ? $order_ids : array( 0 ); 
  325.  
  326. $args['post__in'] = $order_ids; 
  327.  
  328. // Search. 
  329. if ( ! empty( $args['s'] ) ) { 
  330. $order_ids = wc_order_search( $args['s'] ); 
  331.  
  332. if ( ! empty( $order_ids ) ) { 
  333. unset( $args['s'] ); 
  334. $args['post__in'] = array_merge( $order_ids, array( 0 ) ); 
  335.  
  336. return $args; 
  337.  
  338. /** 
  339. * Only reutrn writeable props from schema. 
  340. * @param array $schema 
  341. * @return bool 
  342. */ 
  343. protected function filter_writable_props( $schema ) { 
  344. return empty( $schema['readonly'] ); 
  345.  
  346. /** 
  347. * Prepare a single order for create or update. 
  348. * @param WP_REST_Request $request Request object. 
  349. * @param bool $creating If is creating a new object. 
  350. * @return WP_Error|WC_Data 
  351. */ 
  352. protected function prepare_object_for_database( $request, $creating = false ) { 
  353. $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; 
  354. $order = new WC_Order( $id ); 
  355. $schema = $this->get_item_schema(); 
  356. $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); 
  357.  
  358. // Handle all writable props. 
  359. foreach ( $data_keys as $key ) { 
  360. $value = $request[ $key ]; 
  361.  
  362. if ( ! is_null( $value ) ) { 
  363. switch ( $key ) { 
  364. case 'billing' : 
  365. case 'shipping' : 
  366. $this->update_address( $order, $value, $key ); 
  367. break; 
  368. case 'line_items' : 
  369. case 'shipping_lines' : 
  370. case 'fee_lines' : 
  371. case 'coupon_lines' : 
  372. if ( is_array( $value ) ) { 
  373. foreach ( $value as $item ) { 
  374. if ( is_array( $item ) ) { 
  375. if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) { 
  376. $order->remove_item( $item['id'] ); 
  377. } else { 
  378. $this->set_item( $order, $key, $item ); 
  379. break; 
  380. case 'meta_data' : 
  381. if ( is_array( $value ) ) { 
  382. foreach ( $value as $meta ) { 
  383. $order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); 
  384. break; 
  385. default : 
  386. if ( is_callable( array( $order, "set_{$key}" ) ) ) { 
  387. $order->{"set_{$key}"}( $value ); 
  388. break; 
  389.  
  390. /** 
  391. * Filters an object before it is inserted via the REST API. 
  392. * The dynamic portion of the hook name, `$this->post_type`,  
  393. * refers to the object type slug. 
  394. * @param WC_Data $order Object object. 
  395. * @param WP_REST_Request $request Request object. 
  396. * @param bool $creating If is creating a new object. 
  397. */ 
  398. return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $order, $request, $creating ); 
  399.  
  400. /** 
  401. * Save an object data. 
  402. * @since 3.0.0 
  403. * @param WP_REST_Request $request Full details about the request. 
  404. * @param bool $creating If is creating a new object. 
  405. * @return WC_Data|WP_Error 
  406. */ 
  407. protected function save_object( $request, $creating = false ) { 
  408. try { 
  409. $object = $this->prepare_object_for_database( $request, $creating ); 
  410.  
  411. if ( is_wp_error( $object ) ) { 
  412. return $object; 
  413.  
  414. if ( $creating ) { 
  415. // Make sure customer exists. 
  416. if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] && false === get_user_by( 'id', $request['customer_id'] ) ) { 
  417. throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 ); 
  418.  
  419. $object->set_created_via( 'rest-api' ); 
  420. $object->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); 
  421. $object->calculate_totals(); 
  422.  
  423. $object->save(); 
  424.  
  425. // Actions for after the order is saved. 
  426. if ( $creating ) { 
  427. if ( true === $request['set_paid'] ) { 
  428. $object->payment_complete( $request['transaction_id'] ); 
  429. } else { 
  430. // Handle set paid. 
  431. if ( $object->needs_payment() && true === $request['set_paid'] ) { 
  432. $object->payment_complete( $request['transaction_id'] ); 
  433.  
  434. // If items have changed, recalculate order totals. 
  435. if ( isset( $request['billing'] ) || isset( $request['shipping'] ) || isset( $request['line_items'] ) || isset( $request['shipping_lines'] ) || isset( $request['fee_lines'] ) || isset( $request['coupon_lines'] ) ) { 
  436. $object->calculate_totals(); 
  437.  
  438. return $this->get_object( $object->get_id() ); 
  439. } catch ( WC_Data_Exception $e ) { 
  440. return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); 
  441. } catch ( WC_REST_Exception $e ) { 
  442. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  443.  
  444. /** 
  445. * Update address. 
  446. * @param WC_Order $order 
  447. * @param array $posted 
  448. * @param string $type 
  449. */ 
  450. protected function update_address( $order, $posted, $type = 'billing' ) { 
  451. foreach ( $posted as $key => $value ) { 
  452. if ( is_callable( array( $order, "set_{$type}_{$key}" ) ) ) { 
  453. $order->{"set_{$type}_{$key}"}( $value ); 
  454.  
  455. /** 
  456. * Gets the product ID from the SKU or posted ID. 
  457. * @param array $posted Request data 
  458. * @return int 
  459. */ 
  460. protected function get_product_id( $posted ) { 
  461. if ( ! empty( $posted['sku'] ) ) { 
  462. $product_id = (int) wc_get_product_id_by_sku( $posted['sku'] ); 
  463. } elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) { 
  464. $product_id = (int) $posted['product_id']; 
  465. } elseif ( ! empty( $posted['variation_id'] ) ) { 
  466. $product_id = (int) $posted['variation_id']; 
  467. } else { 
  468. throw new WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce' ), 400 ); 
  469. return $product_id; 
  470.  
  471. /** 
  472. * Maybe set an item prop if the value was posted. 
  473. * @param WC_Order_Item $item 
  474. * @param string $prop 
  475. * @param array $posted Request data. 
  476. */ 
  477. protected function maybe_set_item_prop( $item, $prop, $posted ) { 
  478. if ( isset( $posted[ $prop ] ) ) { 
  479. $item->{"set_$prop"}( $posted[ $prop ] ); 
  480.  
  481. /** 
  482. * Maybe set item props if the values were posted. 
  483. * @param WC_Order_Item $item 
  484. * @param string[] $props 
  485. * @param array $posted Request data. 
  486. */ 
  487. protected function maybe_set_item_props( $item, $props, $posted ) { 
  488. foreach ( $props as $prop ) { 
  489. $this->maybe_set_item_prop( $item, $prop, $posted ); 
  490.  
  491. /** 
  492. * Maybe set item meta if posted. 
  493. * @param WC_Order_Item $item 
  494. * @param array $posted Request data. 
  495. */ 
  496. protected function maybe_set_item_meta_data( $item, $posted ) { 
  497. if ( ! empty( $posted['meta_data'] ) && is_array( $posted['meta_data'] ) ) { 
  498. foreach ( $posted['meta_data'] as $meta ) { 
  499. if ( isset( $meta['key'], $meta['value'] ) ) { 
  500. $item->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); 
  501.  
  502. /** 
  503. * Create or update a line item. 
  504. * @param array $posted Line item data. 
  505. * @param string $action 'create' to add line item or 'update' to update it. 
  506. * @throws WC_REST_Exception Invalid data, server error. 
  507. */ 
  508. protected function prepare_line_items( $posted, $action = 'create' ) { 
  509. $item = new WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' ); 
  510. $product = wc_get_product( $this->get_product_id( $posted ) ); 
  511.  
  512. if ( $product !== $item->get_product() ) { 
  513. $item->set_product( $product ); 
  514.  
  515. if ( 'create' === $action ) { 
  516. $quantity = isset( $posted['quantity'] ) ? $posted['quantity'] : 1; 
  517. $total = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) ); 
  518. $item->set_total( $total ); 
  519. $item->set_subtotal( $total ); 
  520.  
  521. $this->maybe_set_item_props( $item, array( 'name', 'quantity', 'total', 'subtotal', 'tax_class' ), $posted ); 
  522. $this->maybe_set_item_meta_data( $item, $posted ); 
  523.  
  524. return $item; 
  525.  
  526. /** 
  527. * Create or update an order shipping method. 
  528. * @param $posted $shipping Item data. 
  529. * @param string $action 'create' to add shipping or 'update' to update it. 
  530. * @throws WC_REST_Exception Invalid data, server error. 
  531. */ 
  532. protected function prepare_shipping_lines( $posted, $action ) { 
  533. $item = new WC_Order_Item_Shipping( ! empty( $posted['id'] ) ? $posted['id'] : '' ); 
  534.  
  535. if ( 'create' === $action ) { 
  536. if ( empty( $posted['method_id'] ) ) { 
  537. throw new WC_REST_Exception( 'woocommerce_rest_invalid_shipping_item', __( 'Shipping method ID is required.', 'woocommerce' ), 400 ); 
  538.  
  539. $this->maybe_set_item_props( $item, array( 'method_id', 'method_title', 'total' ), $posted ); 
  540. $this->maybe_set_item_meta_data( $item, $posted ); 
  541.  
  542. return $item; 
  543.  
  544. /** 
  545. * Create or update an order fee. 
  546. * @param array $posted Item data. 
  547. * @param string $action 'create' to add fee or 'update' to update it. 
  548. * @throws WC_REST_Exception Invalid data, server error. 
  549. */ 
  550. protected function prepare_fee_lines( $posted, $action ) { 
  551. $item = new WC_Order_Item_Fee( ! empty( $posted['id'] ) ? $posted['id'] : '' ); 
  552.  
  553. if ( 'create' === $action ) { 
  554. if ( empty( $posted['name'] ) ) { 
  555. throw new WC_REST_Exception( 'woocommerce_rest_invalid_fee_item', __( 'Fee name is required.', 'woocommerce' ), 400 ); 
  556.  
  557. $this->maybe_set_item_props( $item, array( 'name', 'tax_class', 'tax_status', 'total' ), $posted ); 
  558. $this->maybe_set_item_meta_data( $item, $posted ); 
  559.  
  560. return $item; 
  561.  
  562. /** 
  563. * Create or update an order coupon. 
  564. * @param array $posted Item data. 
  565. * @param string $action 'create' to add coupon or 'update' to update it. 
  566. * @throws WC_REST_Exception Invalid data, server error. 
  567. */ 
  568. protected function prepare_coupon_lines( $posted, $action ) { 
  569. $item = new WC_Order_Item_Coupon( ! empty( $posted['id'] ) ? $posted['id'] : '' ); 
  570.  
  571. if ( 'create' === $action ) { 
  572. if ( empty( $posted['code'] ) ) { 
  573. throw new WC_REST_Exception( 'woocommerce_rest_invalid_coupon_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 ); 
  574.  
  575. $this->maybe_set_item_props( $item, array( 'code', 'discount' ), $posted ); 
  576. $this->maybe_set_item_meta_data( $item, $posted ); 
  577.  
  578. return $item; 
  579.  
  580. /** 
  581. * Wrapper method to create/update order items. 
  582. * When updating, the item ID provided is checked to ensure it is associated 
  583. * with the order. 
  584. * @param WC_Order $order order 
  585. * @param string $item_type 
  586. * @param array $posted item provided in the request body 
  587. * @throws WC_REST_Exception If item ID is not associated with order 
  588. */ 
  589. protected function set_item( $order, $item_type, $posted ) { 
  590. global $wpdb; 
  591.  
  592. if ( ! empty( $posted['id'] ) ) { 
  593. $action = 'update'; 
  594. } else { 
  595. $action = 'create'; 
  596.  
  597. $method = 'prepare_' . $item_type; 
  598.  
  599. // Verify provided line item ID is associated with order. 
  600. if ( 'update' === $action ) { 
  601. $result = $wpdb->get_row( 
  602. $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",  
  603. absint( $posted['id'] ),  
  604. absint( $order->get_id() ) 
  605. ) ); 
  606. if ( is_null( $result ) ) { 
  607. throw new WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 ); 
  608.  
  609. // Prepare item data 
  610. $item = $this->$method( $posted, $action ); 
  611.  
  612. /** 
  613. * Action hook to adjust item before save. 
  614. * @since 3.0.0 
  615. */ 
  616. do_action( 'woocommerce_rest_set_order_item', $item, $posted ); 
  617.  
  618. // Save or add to order 
  619. if ( 'create' === $action ) { 
  620. $order->add_item( $item ); 
  621. } else { 
  622. $item->save(); 
  623.  
  624. /** 
  625. * Helper method to check if the resource ID associated with the provided item is null. 
  626. * Items can be deleted by setting the resource ID to null. 
  627. * @param array $item Item provided in the request body. 
  628. * @return bool True if the item resource ID is null, false otherwise. 
  629. */ 
  630. protected function item_is_null( $item ) { 
  631. $keys = array( 'product_id', 'method_id', 'method_title', 'name', 'code' ); 
  632.  
  633. foreach ( $keys as $key ) { 
  634. if ( array_key_exists( $key, $item ) && is_null( $item[ $key ] ) ) { 
  635. return true; 
  636.  
  637. return false; 
  638.  
  639. /** 
  640. * Get order statuses without prefixes. 
  641. * @return array 
  642. */ 
  643. protected function get_order_statuses() { 
  644. $order_statuses = array(); 
  645.  
  646. foreach ( array_keys( wc_get_order_statuses() ) as $status ) { 
  647. $order_statuses[] = str_replace( 'wc-', '', $status ); 
  648.  
  649. return $order_statuses; 
  650.  
  651. /** 
  652. * Get the Order's schema, conforming to JSON Schema. 
  653. * @return array 
  654. */ 
  655. public function get_item_schema() { 
  656. $schema = array( 
  657. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  658. 'title' => $this->post_type,  
  659. 'type' => 'object',  
  660. 'properties' => array( 
  661. 'id' => array( 
  662. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  663. 'type' => 'integer',  
  664. 'context' => array( 'view', 'edit' ),  
  665. 'readonly' => true,  
  666. ),  
  667. 'parent_id' => array( 
  668. 'description' => __( 'Parent order ID.', 'woocommerce' ),  
  669. 'type' => 'integer',  
  670. 'context' => array( 'view', 'edit' ),  
  671. ),  
  672. 'number' => array( 
  673. 'description' => __( 'Order number.', 'woocommerce' ),  
  674. 'type' => 'string',  
  675. 'context' => array( 'view', 'edit' ),  
  676. 'readonly' => true,  
  677. ),  
  678. 'order_key' => array( 
  679. 'description' => __( 'Order key.', 'woocommerce' ),  
  680. 'type' => 'string',  
  681. 'context' => array( 'view', 'edit' ),  
  682. 'readonly' => true,  
  683. ),  
  684. 'created_via' => array( 
  685. 'description' => __( 'Shows where the order was created.', 'woocommerce' ),  
  686. 'type' => 'string',  
  687. 'context' => array( 'view', 'edit' ),  
  688. 'readonly' => true,  
  689. ),  
  690. 'version' => array( 
  691. 'description' => __( 'Version of WooCommerce which last updated the order.', 'woocommerce' ),  
  692. 'type' => 'integer',  
  693. 'context' => array( 'view', 'edit' ),  
  694. 'readonly' => true,  
  695. ),  
  696. 'status' => array( 
  697. 'description' => __( 'Order status.', 'woocommerce' ),  
  698. 'type' => 'string',  
  699. 'default' => 'pending',  
  700. 'enum' => $this->get_order_statuses(),  
  701. 'context' => array( 'view', 'edit' ),  
  702. ),  
  703. 'currency' => array( 
  704. 'description' => __( 'Currency the order was created with, in ISO format.', 'woocommerce' ),  
  705. 'type' => 'string',  
  706. 'default' => get_woocommerce_currency(),  
  707. 'enum' => array_keys( get_woocommerce_currencies() ),  
  708. 'context' => array( 'view', 'edit' ),  
  709. ),  
  710. 'date_created' => array( 
  711. 'description' => __( "The date the order was created, in the site's timezone.", 'woocommerce' ),  
  712. 'type' => 'date-time',  
  713. 'context' => array( 'view', 'edit' ),  
  714. 'readonly' => true,  
  715. ),  
  716. 'date_created_gmt' => array( 
  717. 'description' => __( "The date the order was created, as GMT.", 'woocommerce' ),  
  718. 'type' => 'date-time',  
  719. 'context' => array( 'view', 'edit' ),  
  720. 'readonly' => true,  
  721. ),  
  722. 'date_modified' => array( 
  723. 'description' => __( "The date the order was last modified, in the site's timezone.", 'woocommerce' ),  
  724. 'type' => 'date-time',  
  725. 'context' => array( 'view', 'edit' ),  
  726. 'readonly' => true,  
  727. ),  
  728. 'date_modified_gmt' => array( 
  729. 'description' => __( "The date the order was last modified, as GMT.", 'woocommerce' ),  
  730. 'type' => 'date-time',  
  731. 'context' => array( 'view', 'edit' ),  
  732. 'readonly' => true,  
  733. ),  
  734. 'discount_total' => array( 
  735. 'description' => __( 'Total discount amount for the order.', 'woocommerce' ),  
  736. 'type' => 'string',  
  737. 'context' => array( 'view', 'edit' ),  
  738. 'readonly' => true,  
  739. ),  
  740. 'discount_tax' => array( 
  741. 'description' => __( 'Total discount tax amount for the order.', 'woocommerce' ),  
  742. 'type' => 'string',  
  743. 'context' => array( 'view', 'edit' ),  
  744. 'readonly' => true,  
  745. ),  
  746. 'shipping_total' => array( 
  747. 'description' => __( 'Total shipping amount for the order.', 'woocommerce' ),  
  748. 'type' => 'string',  
  749. 'context' => array( 'view', 'edit' ),  
  750. 'readonly' => true,  
  751. ),  
  752. 'shipping_tax' => array( 
  753. 'description' => __( 'Total shipping tax amount for the order.', 'woocommerce' ),  
  754. 'type' => 'string',  
  755. 'context' => array( 'view', 'edit' ),  
  756. 'readonly' => true,  
  757. ),  
  758. 'cart_tax' => array( 
  759. 'description' => __( 'Sum of line item taxes only.', 'woocommerce' ),  
  760. 'type' => 'string',  
  761. 'context' => array( 'view', 'edit' ),  
  762. 'readonly' => true,  
  763. ),  
  764. 'total' => array( 
  765. 'description' => __( 'Grand total.', 'woocommerce' ),  
  766. 'type' => 'string',  
  767. 'context' => array( 'view', 'edit' ),  
  768. 'readonly' => true,  
  769. ),  
  770. 'total_tax' => array( 
  771. 'description' => __( 'Sum of all taxes.', 'woocommerce' ),  
  772. 'type' => 'string',  
  773. 'context' => array( 'view', 'edit' ),  
  774. 'readonly' => true,  
  775. ),  
  776. 'prices_include_tax' => array( 
  777. 'description' => __( 'True the prices included tax during checkout.', 'woocommerce' ),  
  778. 'type' => 'boolean',  
  779. 'context' => array( 'view', 'edit' ),  
  780. 'readonly' => true,  
  781. ),  
  782. 'customer_id' => array( 
  783. 'description' => __( 'User ID who owns the order. 0 for guests.', 'woocommerce' ),  
  784. 'type' => 'integer',  
  785. 'default' => 0,  
  786. 'context' => array( 'view', 'edit' ),  
  787. ),  
  788. 'customer_ip_address' => array( 
  789. 'description' => __( "Customer's IP address.", 'woocommerce' ),  
  790. 'type' => 'string',  
  791. 'context' => array( 'view', 'edit' ),  
  792. 'readonly' => true,  
  793. ),  
  794. 'customer_user_agent' => array( 
  795. 'description' => __( 'User agent of the customer.', 'woocommerce' ),  
  796. 'type' => 'string',  
  797. 'context' => array( 'view', 'edit' ),  
  798. 'readonly' => true,  
  799. ),  
  800. 'customer_note' => array( 
  801. 'description' => __( 'Note left by customer during checkout.', 'woocommerce' ),  
  802. 'type' => 'string',  
  803. 'context' => array( 'view', 'edit' ),  
  804. ),  
  805. 'billing' => array( 
  806. 'description' => __( 'Billing address.', 'woocommerce' ),  
  807. 'type' => 'object',  
  808. 'context' => array( 'view', 'edit' ),  
  809. 'properties' => array( 
  810. 'first_name' => array( 
  811. 'description' => __( 'First name.', 'woocommerce' ),  
  812. 'type' => 'string',  
  813. 'context' => array( 'view', 'edit' ),  
  814. ),  
  815. 'last_name' => array( 
  816. 'description' => __( 'Last name.', 'woocommerce' ),  
  817. 'type' => 'string',  
  818. 'context' => array( 'view', 'edit' ),  
  819. ),  
  820. 'company' => array( 
  821. 'description' => __( 'Company name.', 'woocommerce' ),  
  822. 'type' => 'string',  
  823. 'context' => array( 'view', 'edit' ),  
  824. ),  
  825. 'address_1' => array( 
  826. 'description' => __( 'Address line 1', 'woocommerce' ),  
  827. 'type' => 'string',  
  828. 'context' => array( 'view', 'edit' ),  
  829. ),  
  830. 'address_2' => array( 
  831. 'description' => __( 'Address line 2', 'woocommerce' ),  
  832. 'type' => 'string',  
  833. 'context' => array( 'view', 'edit' ),  
  834. ),  
  835. 'city' => array( 
  836. 'description' => __( 'City name.', 'woocommerce' ),  
  837. 'type' => 'string',  
  838. 'context' => array( 'view', 'edit' ),  
  839. ),  
  840. 'state' => array( 
  841. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),  
  842. 'type' => 'string',  
  843. 'context' => array( 'view', 'edit' ),  
  844. ),  
  845. 'postcode' => array( 
  846. 'description' => __( 'Postal code.', 'woocommerce' ),  
  847. 'type' => 'string',  
  848. 'context' => array( 'view', 'edit' ),  
  849. ),  
  850. 'country' => array( 
  851. 'description' => __( 'Country code in ISO 3166-1 alpha-2 format.', 'woocommerce' ),  
  852. 'type' => 'string',  
  853. 'context' => array( 'view', 'edit' ),  
  854. ),  
  855. 'email' => array( 
  856. 'description' => __( 'Email address.', 'woocommerce' ),  
  857. 'type' => 'string',  
  858. 'format' => 'email',  
  859. 'context' => array( 'view', 'edit' ),  
  860. ),  
  861. 'phone' => array( 
  862. 'description' => __( 'Phone number.', 'woocommerce' ),  
  863. 'type' => 'string',  
  864. 'context' => array( 'view', 'edit' ),  
  865. ),  
  866. ),  
  867. ),  
  868. 'shipping' => array( 
  869. 'description' => __( 'Shipping address.', 'woocommerce' ),  
  870. 'type' => 'object',  
  871. 'context' => array( 'view', 'edit' ),  
  872. 'properties' => array( 
  873. 'first_name' => array( 
  874. 'description' => __( 'First name.', 'woocommerce' ),  
  875. 'type' => 'string',  
  876. 'context' => array( 'view', 'edit' ),  
  877. ),  
  878. 'last_name' => array( 
  879. 'description' => __( 'Last name.', 'woocommerce' ),  
  880. 'type' => 'string',  
  881. 'context' => array( 'view', 'edit' ),  
  882. ),  
  883. 'company' => array( 
  884. 'description' => __( 'Company name.', 'woocommerce' ),  
  885. 'type' => 'string',  
  886. 'context' => array( 'view', 'edit' ),  
  887. ),  
  888. 'address_1' => array( 
  889. 'description' => __( 'Address line 1', 'woocommerce' ),  
  890. 'type' => 'string',  
  891. 'context' => array( 'view', 'edit' ),  
  892. ),  
  893. 'address_2' => array( 
  894. 'description' => __( 'Address line 2', 'woocommerce' ),  
  895. 'type' => 'string',  
  896. 'context' => array( 'view', 'edit' ),  
  897. ),  
  898. 'city' => array( 
  899. 'description' => __( 'City name.', 'woocommerce' ),  
  900. 'type' => 'string',  
  901. 'context' => array( 'view', 'edit' ),  
  902. ),  
  903. 'state' => array( 
  904. 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ),  
  905. 'type' => 'string',  
  906. 'context' => array( 'view', 'edit' ),  
  907. ),  
  908. 'postcode' => array( 
  909. 'description' => __( 'Postal code.', 'woocommerce' ),  
  910. 'type' => 'string',  
  911. 'context' => array( 'view', 'edit' ),  
  912. ),  
  913. 'country' => array( 
  914. 'description' => __( 'Country code in ISO 3166-1 alpha-2 format.', 'woocommerce' ),  
  915. 'type' => 'string',  
  916. 'context' => array( 'view', 'edit' ),  
  917. ),  
  918. ),  
  919. ),  
  920. 'payment_method' => array( 
  921. 'description' => __( 'Payment method ID.', 'woocommerce' ),  
  922. 'type' => 'string',  
  923. 'context' => array( 'view', 'edit' ),  
  924. ),  
  925. 'payment_method_title' => array( 
  926. 'description' => __( 'Payment method title.', 'woocommerce' ),  
  927. 'type' => 'string',  
  928. 'context' => array( 'view', 'edit' ),  
  929. ),  
  930. 'transaction_id' => array( 
  931. 'description' => __( 'Unique transaction ID.', 'woocommerce' ),  
  932. 'type' => 'string',  
  933. 'context' => array( 'view', 'edit' ),  
  934. ),  
  935. 'date_paid' => array( 
  936. 'description' => __( "The date the order was paid, in the site's timezone.", 'woocommerce' ),  
  937. 'type' => 'date-time',  
  938. 'context' => array( 'view', 'edit' ),  
  939. 'readonly' => true,  
  940. ),  
  941. 'date_paid_gmt' => array( 
  942. 'description' => __( "The date the order was paid, as GMT.", 'woocommerce' ),  
  943. 'type' => 'date-time',  
  944. 'context' => array( 'view', 'edit' ),  
  945. 'readonly' => true,  
  946. ),  
  947. 'date_completed' => array( 
  948. 'description' => __( "The date the order was completed, in the site's timezone.", 'woocommerce' ),  
  949. 'type' => 'date-time',  
  950. 'context' => array( 'view', 'edit' ),  
  951. 'readonly' => true,  
  952. ),  
  953. 'date_completed_gmt' => array( 
  954. 'description' => __( "The date the order was completed, as GMT.", 'woocommerce' ),  
  955. 'type' => 'date-time',  
  956. 'context' => array( 'view', 'edit' ),  
  957. 'readonly' => true,  
  958. ),  
  959. 'cart_hash' => array( 
  960. 'description' => __( 'MD5 hash of cart items to ensure orders are not modified.', 'woocommerce' ),  
  961. 'type' => 'string',  
  962. 'context' => array( 'view', 'edit' ),  
  963. 'readonly' => true,  
  964. ),  
  965. 'meta_data' => array( 
  966. 'description' => __( 'Meta data.', 'woocommerce' ),  
  967. 'type' => 'array',  
  968. 'context' => array( 'view', 'edit' ),  
  969. 'items' => array( 
  970. 'type' => 'object',  
  971. 'properties' => array( 
  972. 'id' => array( 
  973. 'description' => __( 'Meta ID.', 'woocommerce' ),  
  974. 'type' => 'integer',  
  975. 'context' => array( 'view', 'edit' ),  
  976. 'readonly' => true,  
  977. ),  
  978. 'key' => array( 
  979. 'description' => __( 'Meta key.', 'woocommerce' ),  
  980. 'type' => 'string',  
  981. 'context' => array( 'view', 'edit' ),  
  982. ),  
  983. 'value' => array( 
  984. 'description' => __( 'Meta value.', 'woocommerce' ),  
  985. 'type' => 'string',  
  986. 'context' => array( 'view', 'edit' ),  
  987. ),  
  988. ),  
  989. ),  
  990. ),  
  991. 'line_items' => array( 
  992. 'description' => __( 'Line items data.', 'woocommerce' ),  
  993. 'type' => 'array',  
  994. 'context' => array( 'view', 'edit' ),  
  995. 'items' => array( 
  996. 'type' => 'object',  
  997. 'properties' => array( 
  998. 'id' => array( 
  999. 'description' => __( 'Item ID.', 'woocommerce' ),  
  1000. 'type' => 'integer',  
  1001. 'context' => array( 'view', 'edit' ),  
  1002. 'readonly' => true,  
  1003. ),  
  1004. 'name' => array( 
  1005. 'description' => __( 'Product name.', 'woocommerce' ),  
  1006. 'type' => 'string',  
  1007. 'context' => array( 'view', 'edit' ),  
  1008. ),  
  1009. 'product_id' => array( 
  1010. 'description' => __( 'Product ID.', 'woocommerce' ),  
  1011. 'type' => 'integer',  
  1012. 'context' => array( 'view', 'edit' ),  
  1013. ),  
  1014. 'variation_id' => array( 
  1015. 'description' => __( 'Variation ID, if applicable.', 'woocommerce' ),  
  1016. 'type' => 'integer',  
  1017. 'context' => array( 'view', 'edit' ),  
  1018. ),  
  1019. 'quantity' => array( 
  1020. 'description' => __( 'Quantity ordered.', 'woocommerce' ),  
  1021. 'type' => 'integer',  
  1022. 'context' => array( 'view', 'edit' ),  
  1023. ),  
  1024. 'tax_class' => array( 
  1025. 'description' => __( 'Tax class of product.', 'woocommerce' ),  
  1026. 'type' => 'integer',  
  1027. 'context' => array( 'view', 'edit' ),  
  1028. ),  
  1029. 'subtotal' => array( 
  1030. 'description' => __( 'Line subtotal (before discounts).', 'woocommerce' ),  
  1031. 'type' => 'string',  
  1032. 'context' => array( 'view', 'edit' ),  
  1033. ),  
  1034. 'subtotal_tax' => array( 
  1035. 'description' => __( 'Line subtotal tax (before discounts).', 'woocommerce' ),  
  1036. 'type' => 'string',  
  1037. 'context' => array( 'view', 'edit' ),  
  1038. 'readonly' => true,  
  1039. ),  
  1040. 'total' => array( 
  1041. 'description' => __( 'Line total (after discounts).', 'woocommerce' ),  
  1042. 'type' => 'string',  
  1043. 'context' => array( 'view', 'edit' ),  
  1044. ),  
  1045. 'total_tax' => array( 
  1046. 'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),  
  1047. 'type' => 'string',  
  1048. 'context' => array( 'view', 'edit' ),  
  1049. 'readonly' => true,  
  1050. ),  
  1051. 'taxes' => array( 
  1052. 'description' => __( 'Line taxes.', 'woocommerce' ),  
  1053. 'type' => 'array',  
  1054. 'context' => array( 'view', 'edit' ),  
  1055. 'readonly' => true,  
  1056. 'items' => array( 
  1057. 'type' => 'object',  
  1058. 'properties' => array( 
  1059. 'id' => array( 
  1060. 'description' => __( 'Tax rate ID.', 'woocommerce' ),  
  1061. 'type' => 'integer',  
  1062. 'context' => array( 'view', 'edit' ),  
  1063. ),  
  1064. 'total' => array( 
  1065. 'description' => __( 'Tax total.', 'woocommerce' ),  
  1066. 'type' => 'string',  
  1067. 'context' => array( 'view', 'edit' ),  
  1068. ),  
  1069. 'subtotal' => array( 
  1070. 'description' => __( 'Tax subtotal.', 'woocommerce' ),  
  1071. 'type' => 'string',  
  1072. 'context' => array( 'view', 'edit' ),  
  1073. ),  
  1074. ),  
  1075. ),  
  1076. ),  
  1077. 'meta_data' => array( 
  1078. 'description' => __( 'Meta data.', 'woocommerce' ),  
  1079. 'type' => 'array',  
  1080. 'context' => array( 'view', 'edit' ),  
  1081. 'items' => array( 
  1082. 'type' => 'object',  
  1083. 'properties' => array( 
  1084. 'id' => array( 
  1085. 'description' => __( 'Meta ID.', 'woocommerce' ),  
  1086. 'type' => 'integer',  
  1087. 'context' => array( 'view', 'edit' ),  
  1088. 'readonly' => true,  
  1089. ),  
  1090. 'key' => array( 
  1091. 'description' => __( 'Meta key.', 'woocommerce' ),  
  1092. 'type' => 'string',  
  1093. 'context' => array( 'view', 'edit' ),  
  1094. ),  
  1095. 'value' => array( 
  1096. 'description' => __( 'Meta value.', 'woocommerce' ),  
  1097. 'type' => 'string',  
  1098. 'context' => array( 'view', 'edit' ),  
  1099. ),  
  1100. ),  
  1101. ),  
  1102. ),  
  1103. 'sku' => array( 
  1104. 'description' => __( 'Product SKU.', 'woocommerce' ),  
  1105. 'type' => 'string',  
  1106. 'context' => array( 'view', 'edit' ),  
  1107. 'readonly' => true,  
  1108. ),  
  1109. 'price' => array( 
  1110. 'description' => __( 'Product price.', 'woocommerce' ),  
  1111. 'type' => 'string',  
  1112. 'context' => array( 'view', 'edit' ),  
  1113. 'readonly' => true,  
  1114. ),  
  1115. ),  
  1116. ),  
  1117. ),  
  1118. 'tax_lines' => array( 
  1119. 'description' => __( 'Tax lines data.', 'woocommerce' ),  
  1120. 'type' => 'array',  
  1121. 'context' => array( 'view', 'edit' ),  
  1122. 'readonly' => true,  
  1123. 'items' => array( 
  1124. 'type' => 'object',  
  1125. 'properties' => array( 
  1126. 'id' => array( 
  1127. 'description' => __( 'Item ID.', 'woocommerce' ),  
  1128. 'type' => 'integer',  
  1129. 'context' => array( 'view', 'edit' ),  
  1130. 'readonly' => true,  
  1131. ),  
  1132. 'rate_code' => array( 
  1133. 'description' => __( 'Tax rate code.', 'woocommerce' ),  
  1134. 'type' => 'string',  
  1135. 'context' => array( 'view', 'edit' ),  
  1136. 'readonly' => true,  
  1137. ),  
  1138. 'rate_id' => array( 
  1139. 'description' => __( 'Tax rate ID.', 'woocommerce' ),  
  1140. 'type' => 'string',  
  1141. 'context' => array( 'view', 'edit' ),  
  1142. 'readonly' => true,  
  1143. ),  
  1144. 'label' => array( 
  1145. 'description' => __( 'Tax rate label.', 'woocommerce' ),  
  1146. 'type' => 'string',  
  1147. 'context' => array( 'view', 'edit' ),  
  1148. 'readonly' => true,  
  1149. ),  
  1150. 'compound' => array( 
  1151. 'description' => __( 'Show if is a compound tax rate.', 'woocommerce' ),  
  1152. 'type' => 'boolean',  
  1153. 'context' => array( 'view', 'edit' ),  
  1154. 'readonly' => true,  
  1155. ),  
  1156. 'tax_total' => array( 
  1157. 'description' => __( 'Tax total (not including shipping taxes).', 'woocommerce' ),  
  1158. 'type' => 'string',  
  1159. 'context' => array( 'view', 'edit' ),  
  1160. 'readonly' => true,  
  1161. ),  
  1162. 'shipping_tax_total' => array( 
  1163. 'description' => __( 'Shipping tax total.', 'woocommerce' ),  
  1164. 'type' => 'string',  
  1165. 'context' => array( 'view', 'edit' ),  
  1166. 'readonly' => true,  
  1167. ),  
  1168. 'meta_data' => array( 
  1169. 'description' => __( 'Meta data.', 'woocommerce' ),  
  1170. 'type' => 'array',  
  1171. 'context' => array( 'view', 'edit' ),  
  1172. 'items' => array( 
  1173. 'type' => 'object',  
  1174. 'properties' => array( 
  1175. 'id' => array( 
  1176. 'description' => __( 'Meta ID.', 'woocommerce' ),  
  1177. 'type' => 'integer',  
  1178. 'context' => array( 'view', 'edit' ),  
  1179. 'readonly' => true,  
  1180. ),  
  1181. 'key' => array( 
  1182. 'description' => __( 'Meta key.', 'woocommerce' ),  
  1183. 'type' => 'string',  
  1184. 'context' => array( 'view', 'edit' ),  
  1185. ),  
  1186. 'value' => array( 
  1187. 'description' => __( 'Meta value.', 'woocommerce' ),  
  1188. 'type' => 'string',  
  1189. 'context' => array( 'view', 'edit' ),  
  1190. ),  
  1191. ),  
  1192. ),  
  1193. ),  
  1194. ),  
  1195. ),  
  1196. ),  
  1197. 'shipping_lines' => array( 
  1198. 'description' => __( 'Shipping lines data.', 'woocommerce' ),  
  1199. 'type' => 'array',  
  1200. 'context' => array( 'view', 'edit' ),  
  1201. 'items' => array( 
  1202. 'type' => 'object',  
  1203. 'properties' => array( 
  1204. 'id' => array( 
  1205. 'description' => __( 'Item ID.', 'woocommerce' ),  
  1206. 'type' => 'integer',  
  1207. 'context' => array( 'view', 'edit' ),  
  1208. 'readonly' => true,  
  1209. ),  
  1210. 'method_title' => array( 
  1211. 'description' => __( 'Shipping method name.', 'woocommerce' ),  
  1212. 'type' => 'string',  
  1213. 'context' => array( 'view', 'edit' ),  
  1214. ),  
  1215. 'method_id' => array( 
  1216. 'description' => __( 'Shipping method ID.', 'woocommerce' ),  
  1217. 'type' => 'string',  
  1218. 'context' => array( 'view', 'edit' ),  
  1219. ),  
  1220. 'total' => array( 
  1221. 'description' => __( 'Line total (after discounts).', 'woocommerce' ),  
  1222. 'type' => 'string',  
  1223. 'context' => array( 'view', 'edit' ),  
  1224. ),  
  1225. 'total_tax' => array( 
  1226. 'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),  
  1227. 'type' => 'string',  
  1228. 'context' => array( 'view', 'edit' ),  
  1229. 'readonly' => true,  
  1230. ),  
  1231. 'taxes' => array( 
  1232. 'description' => __( 'Line taxes.', 'woocommerce' ),  
  1233. 'type' => 'array',  
  1234. 'context' => array( 'view', 'edit' ),  
  1235. 'readonly' => true,  
  1236. 'items' => array( 
  1237. 'type' => 'object',  
  1238. 'properties' => array( 
  1239. 'id' => array( 
  1240. 'description' => __( 'Tax rate ID.', 'woocommerce' ),  
  1241. 'type' => 'integer',  
  1242. 'context' => array( 'view', 'edit' ),  
  1243. 'readonly' => true,  
  1244. ),  
  1245. 'total' => array( 
  1246. 'description' => __( 'Tax total.', 'woocommerce' ),  
  1247. 'type' => 'string',  
  1248. 'context' => array( 'view', 'edit' ),  
  1249. 'readonly' => true,  
  1250. ),  
  1251. ),  
  1252. ),  
  1253. ),  
  1254. 'meta_data' => array( 
  1255. 'description' => __( 'Meta data.', 'woocommerce' ),  
  1256. 'type' => 'array',  
  1257. 'context' => array( 'view', 'edit' ),  
  1258. 'items' => array( 
  1259. 'type' => 'object',  
  1260. 'properties' => array( 
  1261. 'id' => array( 
  1262. 'description' => __( 'Meta ID.', 'woocommerce' ),  
  1263. 'type' => 'integer',  
  1264. 'context' => array( 'view', 'edit' ),  
  1265. 'readonly' => true,  
  1266. ),  
  1267. 'key' => array( 
  1268. 'description' => __( 'Meta key.', 'woocommerce' ),  
  1269. 'type' => 'string',  
  1270. 'context' => array( 'view', 'edit' ),  
  1271. ),  
  1272. 'value' => array( 
  1273. 'description' => __( 'Meta value.', 'woocommerce' ),  
  1274. 'type' => 'string',  
  1275. 'context' => array( 'view', 'edit' ),  
  1276. ),  
  1277. ),  
  1278. ),  
  1279. ),  
  1280. ),  
  1281. ),  
  1282. ),  
  1283. 'fee_lines' => array( 
  1284. 'description' => __( 'Fee lines data.', 'woocommerce' ),  
  1285. 'type' => 'array',  
  1286. 'context' => array( 'view', 'edit' ),  
  1287. 'items' => array( 
  1288. 'type' => 'object',  
  1289. 'properties' => array( 
  1290. 'id' => array( 
  1291. 'description' => __( 'Item ID.', 'woocommerce' ),  
  1292. 'type' => 'integer',  
  1293. 'context' => array( 'view', 'edit' ),  
  1294. 'readonly' => true,  
  1295. ),  
  1296. 'name' => array( 
  1297. 'description' => __( 'Fee name.', 'woocommerce' ),  
  1298. 'type' => 'string',  
  1299. 'context' => array( 'view', 'edit' ),  
  1300. ),  
  1301. 'tax_class' => array( 
  1302. 'description' => __( 'Tax class of fee.', 'woocommerce' ),  
  1303. 'type' => 'string',  
  1304. 'context' => array( 'view', 'edit' ),  
  1305. ),  
  1306. 'tax_status' => array( 
  1307. 'description' => __( 'Tax status of fee.', 'woocommerce' ),  
  1308. 'type' => 'string',  
  1309. 'context' => array( 'view', 'edit' ),  
  1310. 'enum' => array( 'taxable', 'none' ),  
  1311. ),  
  1312. 'total' => array( 
  1313. 'description' => __( 'Line total (after discounts).', 'woocommerce' ),  
  1314. 'type' => 'string',  
  1315. 'context' => array( 'view', 'edit' ),  
  1316. ),  
  1317. 'total_tax' => array( 
  1318. 'description' => __( 'Line total tax (after discounts).', 'woocommerce' ),  
  1319. 'type' => 'string',  
  1320. 'context' => array( 'view', 'edit' ),  
  1321. 'readonly' => true,  
  1322. ),  
  1323. 'taxes' => array( 
  1324. 'description' => __( 'Line taxes.', 'woocommerce' ),  
  1325. 'type' => 'array',  
  1326. 'context' => array( 'view', 'edit' ),  
  1327. 'readonly' => true,  
  1328. 'items' => array( 
  1329. 'type' => 'object',  
  1330. 'properties' => array( 
  1331. 'id' => array( 
  1332. 'description' => __( 'Tax rate ID.', 'woocommerce' ),  
  1333. 'type' => 'integer',  
  1334. 'context' => array( 'view', 'edit' ),  
  1335. 'readonly' => true,  
  1336. ),  
  1337. 'total' => array( 
  1338. 'description' => __( 'Tax total.', 'woocommerce' ),  
  1339. 'type' => 'string',  
  1340. 'context' => array( 'view', 'edit' ),  
  1341. 'readonly' => true,  
  1342. ),  
  1343. 'subtotal' => array( 
  1344. 'description' => __( 'Tax subtotal.', 'woocommerce' ),  
  1345. 'type' => 'string',  
  1346. 'context' => array( 'view', 'edit' ),  
  1347. 'readonly' => true,  
  1348. ),  
  1349. ),  
  1350. ),  
  1351. ),  
  1352. 'meta_data' => array( 
  1353. 'description' => __( 'Meta data.', 'woocommerce' ),  
  1354. 'type' => 'array',  
  1355. 'context' => array( 'view', 'edit' ),  
  1356. 'items' => array( 
  1357. 'type' => 'object',  
  1358. 'properties' => array( 
  1359. 'id' => array( 
  1360. 'description' => __( 'Meta ID.', 'woocommerce' ),  
  1361. 'type' => 'integer',  
  1362. 'context' => array( 'view', 'edit' ),  
  1363. 'readonly' => true,  
  1364. ),  
  1365. 'key' => array( 
  1366. 'description' => __( 'Meta key.', 'woocommerce' ),  
  1367. 'type' => 'string',  
  1368. 'context' => array( 'view', 'edit' ),  
  1369. ),  
  1370. 'value' => array( 
  1371. 'description' => __( 'Meta value.', 'woocommerce' ),  
  1372. 'type' => 'string',  
  1373. 'context' => array( 'view', 'edit' ),  
  1374. ),  
  1375. ),  
  1376. ),  
  1377. ),  
  1378. ),  
  1379. ),  
  1380. ),  
  1381. 'coupon_lines' => array( 
  1382. 'description' => __( 'Coupons line data.', 'woocommerce' ),  
  1383. 'type' => 'array',  
  1384. 'context' => array( 'view', 'edit' ),  
  1385. 'items' => array( 
  1386. 'type' => 'object',  
  1387. 'properties' => array( 
  1388. 'id' => array( 
  1389. 'description' => __( 'Item ID.', 'woocommerce' ),  
  1390. 'type' => 'integer',  
  1391. 'context' => array( 'view', 'edit' ),  
  1392. 'readonly' => true,  
  1393. ),  
  1394. 'code' => array( 
  1395. 'description' => __( 'Coupon code.', 'woocommerce' ),  
  1396. 'type' => 'string',  
  1397. 'context' => array( 'view', 'edit' ),  
  1398. ),  
  1399. 'discount' => array( 
  1400. 'description' => __( 'Discount total.', 'woocommerce' ),  
  1401. 'type' => 'string',  
  1402. 'context' => array( 'view', 'edit' ),  
  1403. ),  
  1404. 'discount_tax' => array( 
  1405. 'description' => __( 'Discount total tax.', 'woocommerce' ),  
  1406. 'type' => 'string',  
  1407. 'context' => array( 'view', 'edit' ),  
  1408. 'readonly' => true,  
  1409. ),  
  1410. 'meta_data' => array( 
  1411. 'description' => __( 'Meta data.', 'woocommerce' ),  
  1412. 'type' => 'array',  
  1413. 'context' => array( 'view', 'edit' ),  
  1414. 'items' => array( 
  1415. 'type' => 'object',  
  1416. 'properties' => array( 
  1417. 'id' => array( 
  1418. 'description' => __( 'Meta ID.', 'woocommerce' ),  
  1419. 'type' => 'integer',  
  1420. 'context' => array( 'view', 'edit' ),  
  1421. 'readonly' => true,  
  1422. ),  
  1423. 'key' => array( 
  1424. 'description' => __( 'Meta key.', 'woocommerce' ),  
  1425. 'type' => 'string',  
  1426. 'context' => array( 'view', 'edit' ),  
  1427. ),  
  1428. 'value' => array( 
  1429. 'description' => __( 'Meta value.', 'woocommerce' ),  
  1430. 'type' => 'string',  
  1431. 'context' => array( 'view', 'edit' ),  
  1432. ),  
  1433. ),  
  1434. ),  
  1435. ),  
  1436. ),  
  1437. ),  
  1438. ),  
  1439. 'refunds' => array( 
  1440. 'description' => __( 'List of refunds.', 'woocommerce' ),  
  1441. 'type' => 'array',  
  1442. 'context' => array( 'view', 'edit' ),  
  1443. 'readonly' => true,  
  1444. 'items' => array( 
  1445. 'type' => 'object',  
  1446. 'properties' => array( 
  1447. 'id' => array( 
  1448. 'description' => __( 'Refund ID.', 'woocommerce' ),  
  1449. 'type' => 'integer',  
  1450. 'context' => array( 'view', 'edit' ),  
  1451. 'readonly' => true,  
  1452. ),  
  1453. 'reason' => array( 
  1454. 'description' => __( 'Refund reason.', 'woocommerce' ),  
  1455. 'type' => 'string',  
  1456. 'context' => array( 'view', 'edit' ),  
  1457. 'readonly' => true,  
  1458. ),  
  1459. 'total' => array( 
  1460. 'description' => __( 'Refund total.', 'woocommerce' ),  
  1461. 'type' => 'string',  
  1462. 'context' => array( 'view', 'edit' ),  
  1463. 'readonly' => true,  
  1464. ),  
  1465. ),  
  1466. ),  
  1467. ),  
  1468. 'set_paid' => array( 
  1469. 'description' => __( 'Define if the order is paid. It will set the status to processing and reduce stock items.', 'woocommerce' ),  
  1470. 'type' => 'boolean',  
  1471. 'default' => false,  
  1472. 'context' => array( 'edit' ),  
  1473. ),  
  1474. ),  
  1475. ); 
  1476.  
  1477. return $this->add_additional_fields_schema( $schema ); 
  1478.  
  1479. /** 
  1480. * Get the query params for collections. 
  1481. * @return array 
  1482. */ 
  1483. public function get_collection_params() { 
  1484. $params = parent::get_collection_params(); 
  1485.  
  1486. $params['status'] = array( 
  1487. 'default' => 'any',  
  1488. 'description' => __( 'Limit result set to orders assigned a specific status.', 'woocommerce' ),  
  1489. 'type' => 'string',  
  1490. 'enum' => array_merge( array( 'any' ), $this->get_order_statuses() ),  
  1491. 'sanitize_callback' => 'sanitize_key',  
  1492. 'validate_callback' => 'rest_validate_request_arg',  
  1493. ); 
  1494. $params['customer'] = array( 
  1495. 'description' => __( 'Limit result set to orders assigned a specific customer.', 'woocommerce' ),  
  1496. 'type' => 'integer',  
  1497. 'sanitize_callback' => 'absint',  
  1498. 'validate_callback' => 'rest_validate_request_arg',  
  1499. ); 
  1500. $params['product'] = array( 
  1501. 'description' => __( 'Limit result set to orders assigned a specific product.', 'woocommerce' ),  
  1502. 'type' => 'integer',  
  1503. 'sanitize_callback' => 'absint',  
  1504. 'validate_callback' => 'rest_validate_request_arg',  
  1505. ); 
  1506. $params['dp'] = array( 
  1507. 'default' => 2,  
  1508. 'description' => __( 'Number of decimal points to use in each resource.', 'woocommerce' ),  
  1509. 'type' => 'integer',  
  1510. 'sanitize_callback' => 'absint',  
  1511. 'validate_callback' => 'rest_validate_request_arg',  
  1512. ); 
  1513.  
  1514. return $params;