WC_REST_Orders_V1_Controller

REST API Orders controller class.

Defined (1)

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

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