/includes/api/v1/class-wc-rest-orders-controller.php

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