/includes/api/legacy/v2/class-wc-api-orders.php

  1. <?php 
  2. /** 
  3. * WooCommerce API Orders Class 
  4. * 
  5. * Handles requests to the /orders endpoint 
  6. * 
  7. * @author WooThemes 
  8. * @category API 
  9. * @package WooCommerce/API 
  10. * @since 2.1 
  11. */ 
  12.  
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; // Exit if accessed directly 
  15.  
  16. class WC_API_Orders extends WC_API_Resource { 
  17.  
  18. /** @var string $base the route base */ 
  19. protected $base = '/orders'; 
  20.  
  21. /** @var string $post_type the custom post type */ 
  22. protected $post_type = 'shop_order'; 
  23.  
  24. /** 
  25. * Register the routes for this class 
  26. * 
  27. * GET|POST /orders 
  28. * GET /orders/count 
  29. * GET|PUT|DELETE /orders/<id> 
  30. * GET /orders/<id>/notes 
  31. * 
  32. * @since 2.1 
  33. * @param array $routes 
  34. * @return array 
  35. */ 
  36. public function register_routes( $routes ) { 
  37.  
  38. # GET|POST /orders 
  39. $routes[ $this->base ] = array( 
  40. array( array( $this, 'get_orders' ), WC_API_Server::READABLE ),  
  41. array( array( $this, 'create_order' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  42. ); 
  43.  
  44. # GET /orders/count 
  45. $routes[ $this->base . '/count' ] = array( 
  46. array( array( $this, 'get_orders_count' ), WC_API_Server::READABLE ),  
  47. ); 
  48.  
  49. # GET /orders/statuses 
  50. $routes[ $this->base . '/statuses' ] = array( 
  51. array( array( $this, 'get_order_statuses' ), WC_API_Server::READABLE ),  
  52. ); 
  53.  
  54. # GET|PUT|DELETE /orders/<id> 
  55. $routes[ $this->base . '/(?P<id>\d+)' ] = array( 
  56. array( array( $this, 'get_order' ), WC_API_Server::READABLE ),  
  57. array( array( $this, 'edit_order' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  58. array( array( $this, 'delete_order' ), WC_API_Server::DELETABLE ),  
  59. ); 
  60.  
  61. # GET|POST /orders/<id>/notes 
  62. $routes[ $this->base . '/(?P<order_id>\d+)/notes' ] = array( 
  63. array( array( $this, 'get_order_notes' ), WC_API_Server::READABLE ),  
  64. array( array( $this, 'create_order_note' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  65. ); 
  66.  
  67. # GET|PUT|DELETE /orders/<order_id>/notes/<id> 
  68. $routes[ $this->base . '/(?P<order_id>\d+)/notes/(?P<id>\d+)' ] = array( 
  69. array( array( $this, 'get_order_note' ), WC_API_Server::READABLE ),  
  70. array( array( $this, 'edit_order_note' ), WC_API_SERVER::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  71. array( array( $this, 'delete_order_note' ), WC_API_SERVER::DELETABLE ),  
  72. ); 
  73.  
  74. # GET|POST /orders/<order_id>/refunds 
  75. $routes[ $this->base . '/(?P<order_id>\d+)/refunds' ] = array( 
  76. array( array( $this, 'get_order_refunds' ), WC_API_Server::READABLE ),  
  77. array( array( $this, 'create_order_refund' ), WC_API_SERVER::CREATABLE | WC_API_Server::ACCEPT_DATA ),  
  78. ); 
  79.  
  80. # GET|PUT|DELETE /orders/<order_id>/refunds/<id> 
  81. $routes[ $this->base . '/(?P<order_id>\d+)/refunds/(?P<id>\d+)' ] = array( 
  82. array( array( $this, 'get_order_refund' ), WC_API_Server::READABLE ),  
  83. array( array( $this, 'edit_order_refund' ), WC_API_SERVER::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  84. array( array( $this, 'delete_order_refund' ), WC_API_SERVER::DELETABLE ),  
  85. ); 
  86.  
  87. # POST|PUT /orders/bulk 
  88. $routes[ $this->base . '/bulk' ] = array( 
  89. array( array( $this, 'bulk' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),  
  90. ); 
  91.  
  92. return $routes; 
  93.  
  94. /** 
  95. * Get all orders 
  96. * 
  97. * @since 2.1 
  98. * @param string $fields 
  99. * @param array $filter 
  100. * @param string $status 
  101. * @param int $page 
  102. * @return array 
  103. */ 
  104. public function get_orders( $fields = null, $filter = array(), $status = null, $page = 1 ) { 
  105.  
  106. if ( ! empty( $status ) ) { 
  107. $filter['status'] = $status; 
  108.  
  109. $filter['page'] = $page; 
  110.  
  111. $query = $this->query_orders( $filter ); 
  112.  
  113. $orders = array(); 
  114.  
  115. foreach ( $query->posts as $order_id ) { 
  116.  
  117. if ( ! $this->is_readable( $order_id ) ) { 
  118. continue; 
  119.  
  120. $orders[] = current( $this->get_order( $order_id, $fields, $filter ) ); 
  121.  
  122. $this->server->add_pagination_headers( $query ); 
  123.  
  124. return array( 'orders' => $orders ); 
  125.  
  126.  
  127. /** 
  128. * Get the order for the given ID 
  129. * 
  130. * @since 2.1 
  131. * @param int $id the order ID 
  132. * @param array $fields 
  133. * @param array $filter 
  134. * @return array 
  135. */ 
  136. public function get_order( $id, $fields = null, $filter = array() ) { 
  137.  
  138. // ensure order ID is valid & user has permission to read 
  139. $id = $this->validate_request( $id, $this->post_type, 'read' ); 
  140.  
  141. if ( is_wp_error( $id ) ) { 
  142. return $id; 
  143.  
  144. // Get the decimal precession 
  145. $dp = ( isset( $filter['dp'] ) ? intval( $filter['dp'] ) : 2 ); 
  146. $order = wc_get_order( $id ); 
  147. $order_data = array( 
  148. 'id' => $order->get_id(),  
  149. 'order_number' => $order->get_order_number(),  
  150. 'created_at' => $this->server->format_datetime( $order->get_date_created() ? $order->get_date_created()->getTimestamp() : 0, false, false ), // API gives UTC times. 
  151. 'updated_at' => $this->server->format_datetime( $order->get_date_modified() ? $order->get_date_modified()->getTimestamp() : 0, false, false ), // API gives UTC times. 
  152. 'completed_at' => $this->server->format_datetime( $order->get_date_completed() ? $order->get_date_completed()->getTimestamp() : 0, false, false ), // API gives UTC times. 
  153. 'status' => $order->get_status(),  
  154. 'currency' => $order->get_currency(),  
  155. 'total' => wc_format_decimal( $order->get_total(), $dp ),  
  156. 'subtotal' => wc_format_decimal( $order->get_subtotal(), $dp ),  
  157. 'total_line_items_quantity' => $order->get_item_count(),  
  158. 'total_tax' => wc_format_decimal( $order->get_total_tax(), $dp ),  
  159. 'total_shipping' => wc_format_decimal( $order->get_shipping_total(), $dp ),  
  160. 'cart_tax' => wc_format_decimal( $order->get_cart_tax(), $dp ),  
  161. 'shipping_tax' => wc_format_decimal( $order->get_shipping_tax(), $dp ),  
  162. 'total_discount' => wc_format_decimal( $order->get_total_discount(), $dp ),  
  163. 'shipping_methods' => $order->get_shipping_method(),  
  164. 'payment_details' => array( 
  165. 'method_id' => $order->get_payment_method(),  
  166. 'method_title' => $order->get_payment_method_title(),  
  167. 'paid' => ! is_null( $order->get_date_paid() ),  
  168. ),  
  169. 'billing_address' => array( 
  170. 'first_name' => $order->get_billing_first_name(),  
  171. 'last_name' => $order->get_billing_last_name(),  
  172. 'company' => $order->get_billing_company(),  
  173. 'address_1' => $order->get_billing_address_1(),  
  174. 'address_2' => $order->get_billing_address_2(),  
  175. 'city' => $order->get_billing_city(),  
  176. 'state' => $order->get_billing_state(),  
  177. 'postcode' => $order->get_billing_postcode(),  
  178. 'country' => $order->get_billing_country(),  
  179. 'email' => $order->get_billing_email(),  
  180. 'phone' => $order->get_billing_phone(),  
  181. ),  
  182. 'shipping_address' => array( 
  183. 'first_name' => $order->get_shipping_first_name(),  
  184. 'last_name' => $order->get_shipping_last_name(),  
  185. 'company' => $order->get_shipping_company(),  
  186. 'address_1' => $order->get_shipping_address_1(),  
  187. 'address_2' => $order->get_shipping_address_2(),  
  188. 'city' => $order->get_shipping_city(),  
  189. 'state' => $order->get_shipping_state(),  
  190. 'postcode' => $order->get_shipping_postcode(),  
  191. 'country' => $order->get_shipping_country(),  
  192. ),  
  193. 'note' => $order->get_customer_note(),  
  194. 'customer_ip' => $order->get_customer_ip_address(),  
  195. 'customer_user_agent' => $order->get_customer_user_agent(),  
  196. 'customer_id' => $order->get_user_id(),  
  197. 'view_order_url' => $order->get_view_order_url(),  
  198. 'line_items' => array(),  
  199. 'shipping_lines' => array(),  
  200. 'tax_lines' => array(),  
  201. 'fee_lines' => array(),  
  202. 'coupon_lines' => array(),  
  203. ); 
  204.  
  205. // add line items 
  206. foreach ( $order->get_items() as $item_id => $item ) { 
  207. $product = $item->get_product(); 
  208. $hideprefix = ( isset( $filter['all_item_meta'] ) && 'true' === $filter['all_item_meta'] ) ? null : '_'; 
  209. $item_meta = $item->get_formatted_meta_data( $hideprefix ); 
  210.  
  211. foreach ( $item_meta as $key => $values ) { 
  212. $item_meta[ $key ]->label = $values->display_key; 
  213. unset( $item_meta[ $key ]->display_key ); 
  214. unset( $item_meta[ $key ]->display_value ); 
  215.  
  216. $order_data['line_items'][] = array( 
  217. 'id' => $item_id,  
  218. 'subtotal' => wc_format_decimal( $order->get_line_subtotal( $item, false, false ), $dp ),  
  219. 'subtotal_tax' => wc_format_decimal( $item->get_subtotal_tax(), $dp ),  
  220. 'total' => wc_format_decimal( $order->get_line_total( $item, false, false ), $dp ),  
  221. 'total_tax' => wc_format_decimal( $item->get_total_tax(), $dp ),  
  222. 'price' => wc_format_decimal( $order->get_item_total( $item, false, false ), $dp ),  
  223. 'quantity' => $item->get_quantity(),  
  224. 'tax_class' => $item->get_tax_class(),  
  225. 'name' => $item->get_name(),  
  226. 'product_id' => $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(),  
  227. 'sku' => is_object( $product ) ? $product->get_sku() : null,  
  228. 'meta' => array_values( $item_meta ),  
  229. ); 
  230.  
  231. // add shipping 
  232. foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) { 
  233. $order_data['shipping_lines'][] = array( 
  234. 'id' => $shipping_item_id,  
  235. 'method_id' => $shipping_item->get_method_id(),  
  236. 'method_title' => $shipping_item->get_name(),  
  237. 'total' => wc_format_decimal( $shipping_item->get_total(), $dp ),  
  238. ); 
  239.  
  240. // add taxes 
  241. foreach ( $order->get_tax_totals() as $tax_code => $tax ) { 
  242. $order_data['tax_lines'][] = array( 
  243. 'id' => $tax->id,  
  244. 'rate_id' => $tax->rate_id,  
  245. 'code' => $tax_code,  
  246. 'title' => $tax->label,  
  247. 'total' => wc_format_decimal( $tax->amount, $dp ),  
  248. 'compound' => (bool) $tax->is_compound,  
  249. ); 
  250.  
  251. // add fees 
  252. foreach ( $order->get_fees() as $fee_item_id => $fee_item ) { 
  253. $order_data['fee_lines'][] = array( 
  254. 'id' => $fee_item_id,  
  255. 'title' => $fee_item->get_name(),  
  256. 'tax_class' => $fee_item->get_tax_class(),  
  257. 'total' => wc_format_decimal( $order->get_line_total( $fee_item ), $dp ),  
  258. 'total_tax' => wc_format_decimal( $order->get_line_tax( $fee_item ), $dp ),  
  259. ); 
  260.  
  261. // add coupons 
  262. foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) { 
  263. $order_data['coupon_lines'][] = array( 
  264. 'id' => $coupon_item_id,  
  265. 'code' => $coupon_item->get_code(),  
  266. 'amount' => wc_format_decimal( $coupon_item->get_discount(), $dp ),  
  267. ); 
  268.  
  269. return array( 'order' => apply_filters( 'woocommerce_api_order_response', $order_data, $order, $fields, $this->server ) ); 
  270.  
  271. /** 
  272. * Get the total number of orders 
  273. * 
  274. * @since 2.4 
  275. * @param string $status 
  276. * @param array $filter 
  277. * @return array 
  278. */ 
  279. public function get_orders_count( $status = null, $filter = array() ) { 
  280.  
  281. try { 
  282. if ( ! current_user_can( 'read_private_shop_orders' ) ) { 
  283. throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_orders_count', __( 'You do not have permission to read the orders count', 'woocommerce' ), 401 ); 
  284.  
  285. if ( ! empty( $status ) ) { 
  286.  
  287. if ( 'any' === $status ) { 
  288.  
  289. $order_statuses = array(); 
  290.  
  291. foreach ( wc_get_order_statuses() as $slug => $name ) { 
  292. $filter['status'] = str_replace( 'wc-', '', $slug ); 
  293. $query = $this->query_orders( $filter ); 
  294. $order_statuses[ str_replace( 'wc-', '', $slug ) ] = (int) $query->found_posts; 
  295.  
  296. return array( 'count' => $order_statuses ); 
  297.  
  298. } else { 
  299. $filter['status'] = $status; 
  300.  
  301. $query = $this->query_orders( $filter ); 
  302.  
  303. return array( 'count' => (int) $query->found_posts ); 
  304.  
  305. } catch ( WC_API_Exception $e ) { 
  306. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  307.  
  308. /** 
  309. * Get a list of valid order statuses 
  310. * 
  311. * Note this requires no specific permissions other than being an authenticated 
  312. * API user. Order statuses (particularly custom statuses) could be considered 
  313. * private information which is why it's not in the API index. 
  314. * 
  315. * @since 2.1 
  316. * @return array 
  317. */ 
  318. public function get_order_statuses() { 
  319.  
  320. $order_statuses = array(); 
  321.  
  322. foreach ( wc_get_order_statuses() as $slug => $name ) { 
  323. $order_statuses[ str_replace( 'wc-', '', $slug ) ] = $name; 
  324.  
  325. return array( 'order_statuses' => apply_filters( 'woocommerce_api_order_statuses_response', $order_statuses, $this ) ); 
  326.  
  327. /** 
  328. * Create an order 
  329. * 
  330. * @since 2.2 
  331. * @param array $data raw order data 
  332. * @return array 
  333. */ 
  334. public function create_order( $data ) { 
  335. global $wpdb; 
  336.  
  337. wc_transaction_query( 'start' ); 
  338.  
  339. try { 
  340. if ( ! isset( $data['order'] ) ) { 
  341. throw new WC_API_Exception( 'woocommerce_api_missing_order_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'order' ), 400 ); 
  342.  
  343. $data = $data['order']; 
  344.  
  345. // permission check 
  346. if ( ! current_user_can( 'publish_shop_orders' ) ) { 
  347. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order', __( 'You do not have permission to create orders', 'woocommerce' ), 401 ); 
  348.  
  349. $data = apply_filters( 'woocommerce_api_create_order_data', $data, $this ); 
  350.  
  351. // default order args, note that status is checked for validity in wc_create_order() 
  352. $default_order_args = array( 
  353. 'status' => isset( $data['status'] ) ? $data['status'] : '',  
  354. 'customer_note' => isset( $data['note'] ) ? $data['note'] : null,  
  355. ); 
  356.  
  357. // if creating order for existing customer 
  358. if ( ! empty( $data['customer_id'] ) ) { 
  359.  
  360. // make sure customer exists 
  361. if ( false === get_user_by( 'id', $data['customer_id'] ) ) { 
  362. throw new WC_API_Exception( 'woocommerce_api_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 ); 
  363.  
  364. $default_order_args['customer_id'] = $data['customer_id']; 
  365.  
  366. // create the pending order 
  367. $order = $this->create_base_order( $default_order_args, $data ); 
  368.  
  369. if ( is_wp_error( $order ) ) { 
  370. throw new WC_API_Exception( 'woocommerce_api_cannot_create_order', sprintf( __( 'Cannot create order: %s', 'woocommerce' ), implode( ', ', $order->get_error_messages() ) ), 400 ); 
  371.  
  372. // billing/shipping addresses 
  373. $this->set_order_addresses( $order, $data ); 
  374.  
  375. $lines = array( 
  376. 'line_item' => 'line_items',  
  377. 'shipping' => 'shipping_lines',  
  378. 'fee' => 'fee_lines',  
  379. 'coupon' => 'coupon_lines',  
  380. ); 
  381.  
  382. foreach ( $lines as $line_type => $line ) { 
  383.  
  384. if ( isset( $data[ $line ] ) && is_array( $data[ $line ] ) ) { 
  385.  
  386. $set_item = "set_{$line_type}"; 
  387.  
  388. foreach ( $data[ $line ] as $item ) { 
  389.  
  390. $this->$set_item( $order, $item, 'create' ); 
  391.  
  392. // calculate totals and set them 
  393. $order->calculate_totals(); 
  394.  
  395. // payment method (and payment_complete() if `paid` == true) 
  396. if ( isset( $data['payment_details'] ) && is_array( $data['payment_details'] ) ) { 
  397.  
  398. // method ID & title are required 
  399. if ( empty( $data['payment_details']['method_id'] ) || empty( $data['payment_details']['method_title'] ) ) { 
  400. throw new WC_API_Exception( 'woocommerce_invalid_payment_details', __( 'Payment method ID and title are required', 'woocommerce' ), 400 ); 
  401.  
  402. update_post_meta( $order->get_id(), '_payment_method', $data['payment_details']['method_id'] ); 
  403. update_post_meta( $order->get_id(), '_payment_method_title', $data['payment_details']['method_title'] ); 
  404.  
  405. // mark as paid if set 
  406. if ( isset( $data['payment_details']['paid'] ) && true === $data['payment_details']['paid'] ) { 
  407. $order->payment_complete( isset( $data['payment_details']['transaction_id'] ) ? $data['payment_details']['transaction_id'] : '' ); 
  408.  
  409. // set order currency 
  410. if ( isset( $data['currency'] ) ) { 
  411.  
  412. if ( ! array_key_exists( $data['currency'], get_woocommerce_currencies() ) ) { 
  413. throw new WC_API_Exception( 'woocommerce_invalid_order_currency', __( 'Provided order currency is invalid.', 'woocommerce' ), 400 ); 
  414.  
  415. update_post_meta( $order->get_id(), '_order_currency', $data['currency'] ); 
  416.  
  417. // set order meta 
  418. if ( isset( $data['order_meta'] ) && is_array( $data['order_meta'] ) ) { 
  419. $this->set_order_meta( $order->get_id(), $data['order_meta'] ); 
  420.  
  421. // HTTP 201 Created 
  422. $this->server->send_status( 201 ); 
  423.  
  424. wc_delete_shop_order_transients( $order ); 
  425.  
  426. do_action( 'woocommerce_api_create_order', $order->get_id(), $data, $this ); 
  427.  
  428. wc_transaction_query( 'commit' ); 
  429.  
  430. return $this->get_order( $order->get_id() ); 
  431.  
  432. } catch ( WC_Data_Exception $e ) { 
  433. wc_transaction_query( 'rollback' ); 
  434. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  435. } catch ( WC_API_Exception $e ) { 
  436. wc_transaction_query( 'rollback' ); 
  437. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  438.  
  439. /** 
  440. * Creates new WC_Order. 
  441. * 
  442. * Requires a separate function for classes that extend WC_API_Orders. 
  443. * 
  444. * @since 2.3 
  445. * @param $args array 
  446. * @return WC_Order 
  447. */ 
  448. protected function create_base_order( $args, $data ) { 
  449. return wc_create_order( $args ); 
  450.  
  451. /** 
  452. * Edit an order 
  453. * 
  454. * @since 2.2 
  455. * @param int $id the order ID 
  456. * @param array $data 
  457. * @return array 
  458. */ 
  459. public function edit_order( $id, $data ) { 
  460. try { 
  461. if ( ! isset( $data['order'] ) ) { 
  462. throw new WC_API_Exception( 'woocommerce_api_missing_order_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'order' ), 400 ); 
  463.  
  464. $data = $data['order']; 
  465.  
  466. $update_totals = false; 
  467.  
  468. $id = $this->validate_request( $id, $this->post_type, 'edit' ); 
  469.  
  470. if ( is_wp_error( $id ) ) { 
  471. return $id; 
  472.  
  473. $data = apply_filters( 'woocommerce_api_edit_order_data', $data, $id, $this ); 
  474. $order = wc_get_order( $id ); 
  475.  
  476. if ( empty( $order ) ) { 
  477. throw new WC_API_Exception( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), 400 ); 
  478.  
  479. $order_args = array( 'order_id' => $order->get_id() ); 
  480.  
  481. // Customer note. 
  482. if ( isset( $data['note'] ) ) { 
  483. $order_args['customer_note'] = $data['note']; 
  484.  
  485. // Customer ID. 
  486. if ( isset( $data['customer_id'] ) && $data['customer_id'] != $order->get_user_id() ) { 
  487. // Make sure customer exists. 
  488. if ( false === get_user_by( 'id', $data['customer_id'] ) ) { 
  489. throw new WC_API_Exception( 'woocommerce_api_invalid_customer_id', __( 'Customer ID is invalid.', 'woocommerce' ), 400 ); 
  490.  
  491. update_post_meta( $order->get_id(), '_customer_user', $data['customer_id'] ); 
  492.  
  493. // Billing/shipping address. 
  494. $this->set_order_addresses( $order, $data ); 
  495.  
  496. $lines = array( 
  497. 'line_item' => 'line_items',  
  498. 'shipping' => 'shipping_lines',  
  499. 'fee' => 'fee_lines',  
  500. 'coupon' => 'coupon_lines',  
  501. ); 
  502.  
  503. foreach ( $lines as $line_type => $line ) { 
  504.  
  505. if ( isset( $data[ $line ] ) && is_array( $data[ $line ] ) ) { 
  506.  
  507. $update_totals = true; 
  508.  
  509. foreach ( $data[ $line ] as $item ) { 
  510.  
  511. // Item ID is always required. 
  512. if ( ! array_key_exists( 'id', $item ) ) { 
  513. $item['id'] = null; 
  514.  
  515. // Create item. 
  516. if ( is_null( $item['id'] ) ) { 
  517. $this->set_item( $order, $line_type, $item, 'create' ); 
  518. } elseif ( $this->item_is_null( $item ) ) { 
  519. // Delete item. 
  520. wc_delete_order_item( $item['id'] ); 
  521. } else { 
  522. // Update item. 
  523. $this->set_item( $order, $line_type, $item, 'update' ); 
  524.  
  525. // Payment method (and payment_complete() if `paid` == true and order needs payment). 
  526. if ( isset( $data['payment_details'] ) && is_array( $data['payment_details'] ) ) { 
  527.  
  528. // Method ID. 
  529. if ( isset( $data['payment_details']['method_id'] ) ) { 
  530. update_post_meta( $order->get_id(), '_payment_method', $data['payment_details']['method_id'] ); 
  531.  
  532. // Method title. 
  533. if ( isset( $data['payment_details']['method_title'] ) ) { 
  534. update_post_meta( $order->get_id(), '_payment_method_title', $data['payment_details']['method_title'] ); 
  535.  
  536. // Mark as paid if set. 
  537. if ( $order->needs_payment() && isset( $data['payment_details']['paid'] ) && true === $data['payment_details']['paid'] ) { 
  538. $order->payment_complete( isset( $data['payment_details']['transaction_id'] ) ? $data['payment_details']['transaction_id'] : '' ); 
  539.  
  540. // Set order currency. 
  541. if ( isset( $data['currency'] ) ) { 
  542. if ( ! array_key_exists( $data['currency'], get_woocommerce_currencies() ) ) { 
  543. throw new WC_API_Exception( 'woocommerce_invalid_order_currency', __( 'Provided order currency is invalid.', 'woocommerce' ), 400 ); 
  544.  
  545. update_post_meta( $order->get_id(), '_order_currency', $data['currency'] ); 
  546.  
  547. // If items have changed, recalculate order totals. 
  548. if ( $update_totals ) { 
  549. $order->calculate_totals(); 
  550.  
  551. // Update order meta. 
  552. if ( isset( $data['order_meta'] ) && is_array( $data['order_meta'] ) ) { 
  553. $this->set_order_meta( $order->get_id(), $data['order_meta'] ); 
  554.  
  555. // Update the order post to set customer note/modified date. 
  556. wc_update_order( $order_args ); 
  557.  
  558. // Order status. 
  559. if ( ! empty( $data['status'] ) ) { 
  560. // Refresh the order instance. 
  561. $order = wc_get_order( $order->get_id() ); 
  562. $order->update_status( $data['status'], isset( $data['status_note'] ) ? $data['status_note'] : '', true ); 
  563.  
  564. wc_delete_shop_order_transients( $order ); 
  565.  
  566. do_action( 'woocommerce_api_edit_order', $order->get_id(), $data, $this ); 
  567.  
  568. return $this->get_order( $id ); 
  569.  
  570. } catch ( WC_Data_Exception $e ) { 
  571. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  572. } catch ( WC_API_Exception $e ) { 
  573. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  574.  
  575. /** 
  576. * Delete an order 
  577. * 
  578. * @param int $id the order ID 
  579. * @param bool $force true to permanently delete order, false to move to trash 
  580. * @return array 
  581. */ 
  582. public function delete_order( $id, $force = false ) { 
  583.  
  584. $id = $this->validate_request( $id, $this->post_type, 'delete' ); 
  585.  
  586. if ( is_wp_error( $id ) ) { 
  587. return $id; 
  588.  
  589. wc_delete_shop_order_transients( $id ); 
  590.  
  591. do_action( 'woocommerce_api_delete_order', $id, $this ); 
  592.  
  593. return $this->delete( $id, 'order', ( 'true' === $force ) ); 
  594.  
  595. /** 
  596. * Helper method to get order post objects 
  597. * 
  598. * @since 2.1 
  599. * @param array $args request arguments for filtering query 
  600. * @return WP_Query 
  601. */ 
  602. protected function query_orders( $args ) { 
  603.  
  604. // set base query arguments 
  605. $query_args = array( 
  606. 'fields' => 'ids',  
  607. 'post_type' => $this->post_type,  
  608. 'post_status' => array_keys( wc_get_order_statuses() ),  
  609. ); 
  610.  
  611. // add status argument 
  612. if ( ! empty( $args['status'] ) ) { 
  613.  
  614. $statuses = 'wc-' . str_replace( ', ', ', wc-', $args['status'] ); 
  615. $statuses = explode( ', ', $statuses ); 
  616. $query_args['post_status'] = $statuses; 
  617.  
  618. unset( $args['status'] ); 
  619.  
  620.  
  621. $query_args = $this->merge_query_args( $query_args, $args ); 
  622.  
  623. return new WP_Query( $query_args ); 
  624.  
  625. /** 
  626. * Helper method to set/update the billing & shipping addresses for 
  627. * an order 
  628. * 
  629. * @since 2.1 
  630. * @param \WC_Order $order 
  631. * @param array $data 
  632. */ 
  633. protected function set_order_addresses( $order, $data ) { 
  634.  
  635. $address_fields = array( 
  636. 'first_name',  
  637. 'last_name',  
  638. 'company',  
  639. 'email',  
  640. 'phone',  
  641. 'address_1',  
  642. 'address_2',  
  643. 'city',  
  644. 'state',  
  645. 'postcode',  
  646. 'country',  
  647. ); 
  648.  
  649. $billing_address = $shipping_address = array(); 
  650.  
  651. // billing address 
  652. if ( isset( $data['billing_address'] ) && is_array( $data['billing_address'] ) ) { 
  653.  
  654. foreach ( $address_fields as $field ) { 
  655.  
  656. if ( isset( $data['billing_address'][ $field ] ) ) { 
  657. $billing_address[ $field ] = wc_clean( $data['billing_address'][ $field ] ); 
  658.  
  659. unset( $address_fields['email'] ); 
  660. unset( $address_fields['phone'] ); 
  661.  
  662. // shipping address 
  663. if ( isset( $data['shipping_address'] ) && is_array( $data['shipping_address'] ) ) { 
  664.  
  665. foreach ( $address_fields as $field ) { 
  666.  
  667. if ( isset( $data['shipping_address'][ $field ] ) ) { 
  668. $shipping_address[ $field ] = wc_clean( $data['shipping_address'][ $field ] ); 
  669.  
  670. $this->update_address( $order, $billing_address, 'billing' ); 
  671. $this->update_address( $order, $shipping_address, 'shipping' ); 
  672.  
  673. // update user meta 
  674. if ( $order->get_user_id() ) { 
  675. foreach ( $billing_address as $key => $value ) { 
  676. update_user_meta( $order->get_user_id(), 'billing_' . $key, $value ); 
  677. foreach ( $shipping_address as $key => $value ) { 
  678. update_user_meta( $order->get_user_id(), 'shipping_' . $key, $value ); 
  679.  
  680. /** 
  681. * Update address. 
  682. * 
  683. * @param WC_Order $order 
  684. * @param array $posted 
  685. * @param string $type 
  686. */ 
  687. protected function update_address( $order, $posted, $type = 'billing' ) { 
  688. foreach ( $posted as $key => $value ) { 
  689. if ( is_callable( array( $order, "set_{$type}_{$key}" ) ) ) { 
  690. $order->{"set_{$type}_{$key}"}( $value ); 
  691.  
  692. /** 
  693. * Helper method to add/update order meta, with two restrictions: 
  694. * 
  695. * 1) Only non-protected meta (no leading underscore) can be set 
  696. * 2) Meta values must be scalar (int, string, bool) 
  697. * 
  698. * @since 2.2 
  699. * @param int $order_id valid order ID 
  700. * @param array $order_meta order meta in array( 'meta_key' => 'meta_value' ) format 
  701. */ 
  702. protected function set_order_meta( $order_id, $order_meta ) { 
  703.  
  704. foreach ( $order_meta as $meta_key => $meta_value ) { 
  705.  
  706. if ( is_string( $meta_key ) && ! is_protected_meta( $meta_key ) && is_scalar( $meta_value ) ) { 
  707. update_post_meta( $order_id, $meta_key, $meta_value ); 
  708.  
  709. /** 
  710. * Helper method to check if the resource ID associated with the provided item is null 
  711. * 
  712. * Items can be deleted by setting the resource ID to null 
  713. * 
  714. * @since 2.2 
  715. * @param array $item item provided in the request body 
  716. * @return bool true if the item resource ID is null, false otherwise 
  717. */ 
  718. protected function item_is_null( $item ) { 
  719.  
  720. $keys = array( 'product_id', 'method_id', 'title', 'code' ); 
  721.  
  722. foreach ( $keys as $key ) { 
  723. if ( array_key_exists( $key, $item ) && is_null( $item[ $key ] ) ) { 
  724. return true; 
  725.  
  726. return false; 
  727.  
  728. /** 
  729. * Wrapper method to create/update order items 
  730. * 
  731. * When updating, the item ID provided is checked to ensure it is associated 
  732. * with the order. 
  733. * 
  734. * @since 2.2 
  735. * @param \WC_Order $order order 
  736. * @param string $item_type 
  737. * @param array $item item provided in the request body 
  738. * @param string $action either 'create' or 'update' 
  739. * @throws WC_API_Exception if item ID is not associated with order 
  740. */ 
  741. protected function set_item( $order, $item_type, $item, $action ) { 
  742. global $wpdb; 
  743.  
  744. $set_method = "set_{$item_type}"; 
  745.  
  746. // verify provided line item ID is associated with order 
  747. if ( 'update' === $action ) { 
  748.  
  749. $result = $wpdb->get_row( 
  750. $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",  
  751. absint( $item['id'] ),  
  752. absint( $order->get_id() ) 
  753. ) ); 
  754.  
  755. if ( is_null( $result ) ) { 
  756. throw new WC_API_Exception( 'woocommerce_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 ); 
  757.  
  758. $this->$set_method( $order, $item, $action ); 
  759.  
  760. /** 
  761. * Create or update a line item 
  762. * 
  763. * @since 2.2 
  764. * @param \WC_Order $order 
  765. * @param array $item line item data 
  766. * @param string $action 'create' to add line item or 'update' to update it 
  767. * @throws WC_API_Exception invalid data, server error 
  768. */ 
  769. protected function set_line_item( $order, $item, $action ) { 
  770. $creating = ( 'create' === $action ); 
  771.  
  772. // product is always required 
  773. if ( ! isset( $item['product_id'] ) && ! isset( $item['sku'] ) ) { 
  774. throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID or SKU is required', 'woocommerce' ), 400 ); 
  775.  
  776. // when updating, ensure product ID provided matches 
  777. if ( 'update' === $action ) { 
  778.  
  779. $item_product_id = wc_get_order_item_meta( $item['id'], '_product_id' ); 
  780. $item_variation_id = wc_get_order_item_meta( $item['id'], '_variation_id' ); 
  781.  
  782. if ( $item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id ) { 
  783. throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID provided does not match this line item', 'woocommerce' ), 400 ); 
  784.  
  785. if ( isset( $item['product_id'] ) ) { 
  786. $product_id = $item['product_id']; 
  787. } elseif ( isset( $item['sku'] ) ) { 
  788. $product_id = wc_get_product_id_by_sku( $item['sku'] ); 
  789.  
  790. // variations must each have a key & value 
  791. $variation_id = 0; 
  792. if ( isset( $item['variations'] ) && is_array( $item['variations'] ) ) { 
  793. foreach ( $item['variations'] as $key => $value ) { 
  794. if ( ! $key || ! $value ) { 
  795. throw new WC_API_Exception( 'woocommerce_api_invalid_product_variation', __( 'The product variation is invalid', 'woocommerce' ), 400 ); 
  796. $variation_id = $this->get_variation_id( wc_get_product( $product_id ), $item['variations'] ); 
  797.  
  798. $product = wc_get_product( $variation_id ? $variation_id : $product_id ); 
  799.  
  800. // must be a valid WC_Product 
  801. if ( ! is_object( $product ) ) { 
  802. throw new WC_API_Exception( 'woocommerce_api_invalid_product', __( 'Product is invalid.', 'woocommerce' ), 400 ); 
  803.  
  804. // quantity must be positive float 
  805. if ( isset( $item['quantity'] ) && floatval( $item['quantity'] ) <= 0 ) { 
  806. throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity must be a positive float.', 'woocommerce' ), 400 ); 
  807.  
  808. // quantity is required when creating 
  809. if ( $creating && ! isset( $item['quantity'] ) ) { 
  810. throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity is required.', 'woocommerce' ), 400 ); 
  811.  
  812. if ( $creating ) { 
  813. $line_item = new WC_Order_Item_Product(); 
  814. } else { 
  815. $line_item = new WC_Order_Item_Product( $item['id'] ); 
  816.  
  817. $line_item->set_product( $product ); 
  818. $line_item->set_order_id( $order->get_id() ); 
  819.  
  820. if ( isset( $item['quantity'] ) ) { 
  821. $line_item->set_quantity( $item['quantity'] ); 
  822. if ( isset( $item['total'] ) ) { 
  823. $line_item->set_total( floatval( $item['total'] ) ); 
  824. } elseif ( $creating ) { 
  825. $total = wc_get_price_excluding_tax( $product, array( 'qty' => $line_item->get_quantity() ) ); 
  826. $line_item->set_total( $total ); 
  827. $line_item->set_subtotal( $total ); 
  828. if ( isset( $item['total_tax'] ) ) { 
  829. $line_item->set_total_tax( floatval( $item['total_tax'] ) ); 
  830. if ( isset( $item['subtotal'] ) ) { 
  831. $line_item->set_subtotal( floatval( $item['subtotal'] ) ); 
  832. if ( isset( $item['subtotal_tax'] ) ) { 
  833. $line_item->set_subtotal_tax( floatval( $item['subtotal_tax'] ) ); 
  834. if ( $variation_id ) { 
  835. $line_item->set_variation_id( $variation_id ); 
  836. $line_item->set_variation( $item['variations'] ); 
  837.  
  838. // Save or add to order. 
  839. if ( $creating ) { 
  840. $order->add_item( $line_item ); 
  841. } else { 
  842. $item_id = $line_item->save(); 
  843.  
  844. if ( ! $item_id ) { 
  845. throw new WC_API_Exception( 'woocommerce_cannot_create_line_item', __( 'Cannot create line item, try again.', 'woocommerce' ), 500 ); 
  846.  
  847. /** 
  848. * Given a product ID & API provided variations, find the correct variation ID to use for calculation 
  849. * We can't just trust input from the API to pass a variation_id manually, otherwise you could pass 
  850. * the cheapest variation ID but provide other information so we have to look up the variation ID. 
  851. * @param int $product_id main product ID 
  852. * @return int returns an ID if a valid variation was found for this product 
  853. */ 
  854. function get_variation_id( $product, $variations = array() ) { 
  855. $variation_id = null; 
  856. $variations_normalized = array(); 
  857.  
  858. if ( $product->is_type( 'variable' ) && $product->has_child() ) { 
  859. if ( isset( $variations ) && is_array( $variations ) ) { 
  860. // start by normalizing the passed variations 
  861. foreach ( $variations as $key => $value ) { 
  862. $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); // from get_attributes in class-wc-api-products.php 
  863. $variations_normalized[ $key ] = strtolower( $value ); 
  864. // now search through each product child and see if our passed variations match anything 
  865. foreach ( $product->get_children() as $variation ) { 
  866. $meta = array(); 
  867. foreach ( get_post_meta( $variation ) as $key => $value ) { 
  868. $value = $value[0]; 
  869. $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); 
  870. $meta[ $key ] = strtolower( $value ); 
  871. // if the variation array is a part of the $meta array, we found our match 
  872. if ( $this->array_contains( $variations_normalized, $meta ) ) { 
  873. $variation_id = $variation; 
  874. break; 
  875.  
  876. return $variation_id; 
  877.  
  878. /** 
  879. * Utility function to see if the meta array contains data from variations 
  880. */ 
  881. protected function array_contains( $needles, $haystack ) { 
  882. foreach ( $needles as $key => $value ) { 
  883. if ( $haystack[ $key ] !== $value ) { 
  884. return false; 
  885. return true; 
  886.  
  887. /** 
  888. * Create or update an order shipping method 
  889. * 
  890. * @since 2.2 
  891. * @param \WC_Order $order 
  892. * @param array $shipping item data 
  893. * @param string $action 'create' to add shipping or 'update' to update it 
  894. * @throws WC_API_Exception invalid data, server error 
  895. */ 
  896. protected function set_shipping( $order, $shipping, $action ) { 
  897.  
  898. // total must be a positive float 
  899. if ( isset( $shipping['total'] ) && floatval( $shipping['total'] ) < 0 ) { 
  900. throw new WC_API_Exception( 'woocommerce_invalid_shipping_total', __( 'Shipping total must be a positive amount.', 'woocommerce' ), 400 ); 
  901.  
  902. if ( 'create' === $action ) { 
  903.  
  904. // method ID is required 
  905. if ( ! isset( $shipping['method_id'] ) ) { 
  906. throw new WC_API_Exception( 'woocommerce_invalid_shipping_item', __( 'Shipping method ID is required.', 'woocommerce' ), 400 ); 
  907.  
  908. $rate = new WC_Shipping_Rate( $shipping['method_id'], isset( $shipping['method_title'] ) ? $shipping['method_title'] : '', isset( $shipping['total'] ) ? floatval( $shipping['total'] ) : 0, array(), $shipping['method_id'] ); 
  909. $item = new WC_Order_Item_Shipping(); 
  910. $item->set_order_id( $order->get_id() ); 
  911. $item->set_shipping_rate( $rate ); 
  912. $order->add_item( $item ); 
  913. } else { 
  914.  
  915. $item = new WC_Order_Item_Shipping( $shipping['id'] ); 
  916.  
  917. if ( isset( $shipping['method_id'] ) ) { 
  918. $item->set_method_id( $shipping['method_id'] ); 
  919.  
  920. if ( isset( $shipping['method_title'] ) ) { 
  921. $item->set_method_title( $shipping['method_title'] ); 
  922.  
  923. if ( isset( $shipping['total'] ) ) { 
  924. $item->set_total( floatval( $shipping['total'] ) ); 
  925.  
  926. $shipping_id = $item->save(); 
  927.  
  928. if ( ! $shipping_id ) { 
  929. throw new WC_API_Exception( 'woocommerce_cannot_update_shipping', __( 'Cannot update shipping method, try again.', 'woocommerce' ), 500 ); 
  930.  
  931. /** 
  932. * Create or update an order fee 
  933. * 
  934. * @since 2.2 
  935. * @param \WC_Order $order 
  936. * @param array $fee item data 
  937. * @param string $action 'create' to add fee or 'update' to update it 
  938. * @throws WC_API_Exception invalid data, server error 
  939. */ 
  940. protected function set_fee( $order, $fee, $action ) { 
  941.  
  942. if ( 'create' === $action ) { 
  943.  
  944. // fee title is required 
  945. if ( ! isset( $fee['title'] ) ) { 
  946. throw new WC_API_Exception( 'woocommerce_invalid_fee_item', __( 'Fee title is required', 'woocommerce' ), 400 ); 
  947.  
  948. $item = new WC_Order_Item_Fee(); 
  949. $item->set_order_id( $order->get_id() ); 
  950. $item->set_name( wc_clean( $fee['title'] ) ); 
  951. $item->set_total( isset( $fee['total'] ) ? floatval( $fee['total'] ) : 0 ); 
  952.  
  953. // if taxable, tax class and total are required 
  954. if ( ! empty( $fee['taxable'] ) ) { 
  955. if ( ! isset( $fee['tax_class'] ) ) { 
  956. throw new WC_API_Exception( 'woocommerce_invalid_fee_item', __( 'Fee tax class is required when fee is taxable.', 'woocommerce' ), 400 ); 
  957.  
  958. $item->set_tax_status( 'taxable' ); 
  959. $item->set_tax_class( $fee['tax_class'] ); 
  960.  
  961. if ( isset( $fee['total_tax'] ) ) { 
  962. $item->set_total_tax( isset( $fee['total_tax'] ) ? wc_format_refund_total( $fee['total_tax'] ) : 0 ); 
  963.  
  964. if ( isset( $fee['tax_data'] ) ) { 
  965. $item->set_total_tax( wc_format_refund_total( array_sum( $fee['tax_data'] ) ) ); 
  966. $item->set_taxes( array_map( 'wc_format_refund_total', $fee['tax_data'] ) ); 
  967.  
  968. $order->add_item( $item ); 
  969. } else { 
  970.  
  971. $item = new WC_Order_Item_Fee( $fee['id'] ); 
  972.  
  973. if ( isset( $fee['title'] ) ) { 
  974. $item->set_name( wc_clean( $fee['title'] ) ); 
  975.  
  976. if ( isset( $fee['tax_class'] ) ) { 
  977. $item->set_tax_class( $fee['tax_class'] ); 
  978.  
  979. if ( isset( $fee['total'] ) ) { 
  980. $item->set_total( floatval( $fee['total'] ) ); 
  981.  
  982. if ( isset( $fee['total_tax'] ) ) { 
  983. $item->set_total_tax( floatval( $fee['total_tax'] ) ); 
  984.  
  985. $fee_id = $item->save(); 
  986.  
  987. if ( ! $fee_id ) { 
  988. throw new WC_API_Exception( 'woocommerce_cannot_update_fee', __( 'Cannot update fee, try again.', 'woocommerce' ), 500 ); 
  989.  
  990. /** 
  991. * Create or update an order coupon 
  992. * 
  993. * @since 2.2 
  994. * @param \WC_Order $order 
  995. * @param array $coupon item data 
  996. * @param string $action 'create' to add coupon or 'update' to update it 
  997. * @throws WC_API_Exception invalid data, server error 
  998. */ 
  999. protected function set_coupon( $order, $coupon, $action ) { 
  1000.  
  1001. // coupon amount must be positive float 
  1002. if ( isset( $coupon['amount'] ) && floatval( $coupon['amount'] ) < 0 ) { 
  1003. throw new WC_API_Exception( 'woocommerce_invalid_coupon_total', __( 'Coupon discount total must be a positive amount.', 'woocommerce' ), 400 ); 
  1004.  
  1005. if ( 'create' === $action ) { 
  1006.  
  1007. // coupon code is required 
  1008. if ( empty( $coupon['code'] ) ) { 
  1009. throw new WC_API_Exception( 'woocommerce_invalid_coupon_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 ); 
  1010.  
  1011. $item = new WC_Order_Item_Coupon(); 
  1012. $item->set_props( array( 
  1013. 'code' => $coupon['code'],  
  1014. 'discount' => isset( $coupon['amount'] ) ? floatval( $coupon['amount'] ) : 0,  
  1015. 'discount_tax' => 0,  
  1016. 'order_id' => $order->get_id(),  
  1017. ) ); 
  1018. $order->add_item( $item ); 
  1019. } else { 
  1020.  
  1021. $item = new WC_Order_Item_Coupon( $coupon['id'] ); 
  1022.  
  1023. if ( isset( $coupon['code'] ) ) { 
  1024. $item->set_code( $coupon['code'] ); 
  1025.  
  1026. if ( isset( $coupon['amount'] ) ) { 
  1027. $item->set_discount( floatval( $coupon['amount'] ) ); 
  1028.  
  1029. $coupon_id = $item->save(); 
  1030.  
  1031. if ( ! $coupon_id ) { 
  1032. throw new WC_API_Exception( 'woocommerce_cannot_update_order_coupon', __( 'Cannot update coupon, try again.', 'woocommerce' ), 500 ); 
  1033.  
  1034. /** 
  1035. * Get the admin order notes for an order 
  1036. * 
  1037. * @since 2.1 
  1038. * @param string $order_id order ID 
  1039. * @param string|null $fields fields to include in response 
  1040. * @return array 
  1041. */ 
  1042. public function get_order_notes( $order_id, $fields = null ) { 
  1043.  
  1044. // ensure ID is valid order ID 
  1045. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1046.  
  1047. if ( is_wp_error( $order_id ) ) { 
  1048. return $order_id; 
  1049.  
  1050. $args = array( 
  1051. 'post_id' => $order_id,  
  1052. 'approve' => 'approve',  
  1053. 'type' => 'order_note',  
  1054. ); 
  1055.  
  1056. remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); 
  1057.  
  1058. $notes = get_comments( $args ); 
  1059.  
  1060. add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); 
  1061.  
  1062. $order_notes = array(); 
  1063.  
  1064. foreach ( $notes as $note ) { 
  1065.  
  1066. $order_notes[] = current( $this->get_order_note( $order_id, $note->comment_ID, $fields ) ); 
  1067.  
  1068. return array( 'order_notes' => apply_filters( 'woocommerce_api_order_notes_response', $order_notes, $order_id, $fields, $notes, $this->server ) ); 
  1069.  
  1070. /** 
  1071. * Get an order note for the given order ID and ID 
  1072. * 
  1073. * @since 2.2 
  1074. * @param string $order_id order ID 
  1075. * @param string $id order note ID 
  1076. * @param string|null $fields fields to limit response to 
  1077. * @return array 
  1078. */ 
  1079. public function get_order_note( $order_id, $id, $fields = null ) { 
  1080. try { 
  1081. // Validate order ID 
  1082. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1083.  
  1084. if ( is_wp_error( $order_id ) ) { 
  1085. return $order_id; 
  1086.  
  1087. $id = absint( $id ); 
  1088.  
  1089. if ( empty( $id ) ) { 
  1090. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 ); 
  1091.  
  1092. $note = get_comment( $id ); 
  1093.  
  1094. if ( is_null( $note ) ) { 
  1095. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 ); 
  1096.  
  1097. $order_note = array( 
  1098. 'id' => $note->comment_ID,  
  1099. 'created_at' => $this->server->format_datetime( $note->comment_date_gmt ),  
  1100. 'note' => $note->comment_content,  
  1101. 'customer_note' => get_comment_meta( $note->comment_ID, 'is_customer_note', true ) ? true : false,  
  1102. ); 
  1103.  
  1104. return array( 'order_note' => apply_filters( 'woocommerce_api_order_note_response', $order_note, $id, $fields, $note, $order_id, $this ) ); 
  1105. } catch ( WC_API_Exception $e ) { 
  1106. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1107.  
  1108. /** 
  1109. * Create a new order note for the given order 
  1110. * 
  1111. * @since 2.2 
  1112. * @param string $order_id order ID 
  1113. * @param array $data raw request data 
  1114. * @return WP_Error|array error or created note response data 
  1115. */ 
  1116. public function create_order_note( $order_id, $data ) { 
  1117. try { 
  1118. if ( ! isset( $data['order_note'] ) ) { 
  1119. throw new WC_API_Exception( 'woocommerce_api_missing_order_note_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'order_note' ), 400 ); 
  1120.  
  1121. $data = $data['order_note']; 
  1122.  
  1123. // permission check 
  1124. if ( ! current_user_can( 'publish_shop_orders' ) ) { 
  1125. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_note', __( 'You do not have permission to create order notes', 'woocommerce' ), 401 ); 
  1126.  
  1127. $order_id = $this->validate_request( $order_id, $this->post_type, 'edit' ); 
  1128.  
  1129. if ( is_wp_error( $order_id ) ) { 
  1130. return $order_id; 
  1131.  
  1132. $order = wc_get_order( $order_id ); 
  1133.  
  1134. $data = apply_filters( 'woocommerce_api_create_order_note_data', $data, $order_id, $this ); 
  1135.  
  1136. // note content is required 
  1137. if ( ! isset( $data['note'] ) ) { 
  1138. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note', __( 'Order note is required', 'woocommerce' ), 400 ); 
  1139.  
  1140. $is_customer_note = ( isset( $data['customer_note'] ) && true === $data['customer_note'] ); 
  1141.  
  1142. // create the note 
  1143. $note_id = $order->add_order_note( $data['note'], $is_customer_note ); 
  1144.  
  1145. if ( ! $note_id ) { 
  1146. throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again.', 'woocommerce' ), 500 ); 
  1147.  
  1148. // HTTP 201 Created 
  1149. $this->server->send_status( 201 ); 
  1150.  
  1151. do_action( 'woocommerce_api_create_order_note', $note_id, $order_id, $this ); 
  1152.  
  1153. return $this->get_order_note( $order->get_id(), $note_id ); 
  1154. } catch ( WC_Data_Exception $e ) { 
  1155. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1156. } catch ( WC_API_Exception $e ) { 
  1157. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1158.  
  1159. /** 
  1160. * Edit the order note 
  1161. * 
  1162. * @since 2.2 
  1163. * @param string $order_id order ID 
  1164. * @param string $id note ID 
  1165. * @param array $data parsed request data 
  1166. * @return WP_Error|array error or edited note response data 
  1167. */ 
  1168. public function edit_order_note( $order_id, $id, $data ) { 
  1169. try { 
  1170. if ( ! isset( $data['order_note'] ) ) { 
  1171. throw new WC_API_Exception( 'woocommerce_api_missing_order_note_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'order_note' ), 400 ); 
  1172.  
  1173. $data = $data['order_note']; 
  1174.  
  1175. // Validate order ID 
  1176. $order_id = $this->validate_request( $order_id, $this->post_type, 'edit' ); 
  1177.  
  1178. if ( is_wp_error( $order_id ) ) { 
  1179. return $order_id; 
  1180.  
  1181. $order = wc_get_order( $order_id ); 
  1182.  
  1183. // Validate note ID 
  1184. $id = absint( $id ); 
  1185.  
  1186. if ( empty( $id ) ) { 
  1187. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 ); 
  1188.  
  1189. // Ensure note ID is valid 
  1190. $note = get_comment( $id ); 
  1191.  
  1192. if ( is_null( $note ) ) { 
  1193. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 ); 
  1194.  
  1195. // Ensure note ID is associated with given order 
  1196. if ( $note->comment_post_ID != $order->get_id() ) { 
  1197. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), 400 ); 
  1198.  
  1199. $data = apply_filters( 'woocommerce_api_edit_order_note_data', $data, $note->comment_ID, $order->get_id(), $this ); 
  1200.  
  1201. // Note content 
  1202. if ( isset( $data['note'] ) ) { 
  1203.  
  1204. wp_update_comment( 
  1205. array( 
  1206. 'comment_ID' => $note->comment_ID,  
  1207. 'comment_content' => $data['note'],  
  1208. ); 
  1209.  
  1210. // Customer note 
  1211. if ( isset( $data['customer_note'] ) ) { 
  1212.  
  1213. update_comment_meta( $note->comment_ID, 'is_customer_note', true === $data['customer_note'] ? 1 : 0 ); 
  1214.  
  1215. do_action( 'woocommerce_api_edit_order_note', $note->comment_ID, $order->get_id(), $this ); 
  1216.  
  1217. return $this->get_order_note( $order->get_id(), $note->comment_ID ); 
  1218. } catch ( WC_Data_Exception $e ) { 
  1219. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1220. } catch ( WC_API_Exception $e ) { 
  1221. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1222.  
  1223. /** 
  1224. * Delete order note 
  1225. * 
  1226. * @since 2.2 
  1227. * @param string $order_id order ID 
  1228. * @param string $id note ID 
  1229. * @return WP_Error|array error or deleted message 
  1230. */ 
  1231. public function delete_order_note( $order_id, $id ) { 
  1232. try { 
  1233. $order_id = $this->validate_request( $order_id, $this->post_type, 'delete' ); 
  1234.  
  1235. if ( is_wp_error( $order_id ) ) { 
  1236. return $order_id; 
  1237.  
  1238. // Validate note ID 
  1239. $id = absint( $id ); 
  1240.  
  1241. if ( empty( $id ) ) { 
  1242. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 ); 
  1243.  
  1244. // Ensure note ID is valid 
  1245. $note = get_comment( $id ); 
  1246.  
  1247. if ( is_null( $note ) ) { 
  1248. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 ); 
  1249.  
  1250. // Ensure note ID is associated with given order 
  1251. if ( $note->comment_post_ID != $order_id ) { 
  1252. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), 400 ); 
  1253.  
  1254. // Force delete since trashed order notes could not be managed through comments list table 
  1255. $result = wp_delete_comment( $note->comment_ID, true ); 
  1256.  
  1257. if ( ! $result ) { 
  1258. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_order_note', __( 'This order note cannot be deleted', 'woocommerce' ), 500 ); 
  1259.  
  1260. do_action( 'woocommerce_api_delete_order_note', $note->comment_ID, $order_id, $this ); 
  1261.  
  1262. return array( 'message' => __( 'Permanently deleted order note', 'woocommerce' ) ); 
  1263. } catch ( WC_API_Exception $e ) { 
  1264. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1265.  
  1266. /** 
  1267. * Get the order refunds for an order 
  1268. * 
  1269. * @since 2.2 
  1270. * @param string $order_id order ID 
  1271. * @param string|null $fields fields to include in response 
  1272. * @return array 
  1273. */ 
  1274. public function get_order_refunds( $order_id, $fields = null ) { 
  1275.  
  1276. // Ensure ID is valid order ID 
  1277. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1278.  
  1279. if ( is_wp_error( $order_id ) ) { 
  1280. return $order_id; 
  1281.  
  1282. $refund_items = wc_get_orders( array( 
  1283. 'type' => 'shop_order_refund',  
  1284. 'parent' => $order_id,  
  1285. 'limit' => -1,  
  1286. 'return' => 'ids',  
  1287. ) ); 
  1288. $order_refunds = array(); 
  1289.  
  1290. foreach ( $refund_items as $refund_id ) { 
  1291. $order_refunds[] = current( $this->get_order_refund( $order_id, $refund_id, $fields ) ); 
  1292.  
  1293. return array( 'order_refunds' => apply_filters( 'woocommerce_api_order_refunds_response', $order_refunds, $order_id, $fields, $refund_items, $this ) ); 
  1294.  
  1295. /** 
  1296. * Get an order refund for the given order ID and ID 
  1297. * 
  1298. * @since 2.2 
  1299. * @param string $order_id order ID 
  1300. * @param string|null $fields fields to limit response to 
  1301. * @return array 
  1302. */ 
  1303. public function get_order_refund( $order_id, $id, $fields = null ) { 
  1304. try { 
  1305. // Validate order ID 
  1306. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1307.  
  1308. if ( is_wp_error( $order_id ) ) { 
  1309. return $order_id; 
  1310.  
  1311. $id = absint( $id ); 
  1312.  
  1313. if ( empty( $id ) ) { 
  1314. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 ); 
  1315.  
  1316. $order = wc_get_order( $order_id ); 
  1317. $refund = wc_get_order( $id ); 
  1318.  
  1319. if ( ! $refund ) { 
  1320. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found.', 'woocommerce' ), 404 ); 
  1321.  
  1322. $line_items = array(); 
  1323.  
  1324. // Add line items 
  1325. foreach ( $refund->get_items( 'line_item' ) as $item_id => $item ) { 
  1326. $product = $item->get_product(); 
  1327. $hideprefix = ( isset( $filter['all_item_meta'] ) && 'true' === $filter['all_item_meta'] ) ? null : '_'; 
  1328. $item_meta = $item->get_formatted_meta_data( $hideprefix ); 
  1329.  
  1330. foreach ( $item_meta as $key => $values ) { 
  1331. $item_meta[ $key ]->label = $values->display_key; 
  1332. unset( $item_meta[ $key ]->display_key ); 
  1333. unset( $item_meta[ $key ]->display_value ); 
  1334.  
  1335. $line_items[] = array( 
  1336. 'id' => $item_id,  
  1337. 'subtotal' => wc_format_decimal( $order->get_line_subtotal( $item ), 2 ),  
  1338. 'subtotal_tax' => wc_format_decimal( $item->get_subtotal_tax(), 2 ),  
  1339. 'total' => wc_format_decimal( $order->get_line_total( $item ), 2 ),  
  1340. 'total_tax' => wc_format_decimal( $order->get_line_tax( $item ), 2 ),  
  1341. 'price' => wc_format_decimal( $order->get_item_total( $item ), 2 ),  
  1342. 'quantity' => $item->get_quantity(),  
  1343. 'tax_class' => $item->get_tax_class(),  
  1344. 'name' => $item->get_name(),  
  1345. 'product_id' => $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(),  
  1346. 'sku' => is_object( $product ) ? $product->get_sku() : null,  
  1347. 'meta' => array_values( $item_meta ),  
  1348. 'refunded_item_id' => (int) $item->get_meta( 'refunded_item_id' ),  
  1349. ); 
  1350.  
  1351. $order_refund = array( 
  1352. 'id' => $refund->id,  
  1353. 'created_at' => $this->server->format_datetime( $refund->get_date_created() ? $refund->get_date_created()->getTimestamp() : 0, false, false ),  
  1354. 'amount' => wc_format_decimal( $refund->get_amount(), 2 ),  
  1355. 'reason' => $refund->get_reason(),  
  1356. 'line_items' => $line_items,  
  1357. ); 
  1358.  
  1359. return array( 'order_refund' => apply_filters( 'woocommerce_api_order_refund_response', $order_refund, $id, $fields, $refund, $order_id, $this ) ); 
  1360. } catch ( WC_API_Exception $e ) { 
  1361. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1362.  
  1363. /** 
  1364. * Create a new order refund for the given order 
  1365. * 
  1366. * @since 2.2 
  1367. * @param string $order_id order ID 
  1368. * @param array $data raw request data 
  1369. * @param bool $api_refund do refund using a payment gateway API 
  1370. * @return WP_Error|array error or created refund response data 
  1371. */ 
  1372. public function create_order_refund( $order_id, $data, $api_refund = true ) { 
  1373. try { 
  1374. if ( ! isset( $data['order_refund'] ) ) { 
  1375. throw new WC_API_Exception( 'woocommerce_api_missing_order_refund_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'order_refund' ), 400 ); 
  1376.  
  1377. $data = $data['order_refund']; 
  1378.  
  1379. // Permission check 
  1380. if ( ! current_user_can( 'publish_shop_orders' ) ) { 
  1381. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_refund', __( 'You do not have permission to create order refunds', 'woocommerce' ), 401 ); 
  1382.  
  1383. $order_id = absint( $order_id ); 
  1384.  
  1385. if ( empty( $order_id ) ) { 
  1386. throw new WC_API_Exception( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), 400 ); 
  1387.  
  1388. $data = apply_filters( 'woocommerce_api_create_order_refund_data', $data, $order_id, $this ); 
  1389.  
  1390. // Refund amount is required 
  1391. if ( ! isset( $data['amount'] ) ) { 
  1392. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount is required.', 'woocommerce' ), 400 ); 
  1393. } elseif ( 0 > $data['amount'] ) { 
  1394. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount must be positive.', 'woocommerce' ), 400 ); 
  1395.  
  1396. $data['order_id'] = $order_id; 
  1397. $data['refund_id'] = 0; 
  1398.  
  1399. // Create the refund 
  1400. $refund = wc_create_refund( $data ); 
  1401.  
  1402. if ( ! $refund ) { 
  1403. throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 ); 
  1404.  
  1405. // Refund via API 
  1406. if ( $api_refund ) { 
  1407. if ( WC()->payment_gateways() ) { 
  1408. $payment_gateways = WC()->payment_gateways->payment_gateways(); 
  1409.  
  1410. $order = wc_get_order( $order_id ); 
  1411.  
  1412. if ( isset( $payment_gateways[ $order->get_payment_method() ] ) && $payment_gateways[ $order->get_payment_method() ]->supports( 'refunds' ) ) { 
  1413. $result = $payment_gateways[ $order->get_payment_method() ]->process_refund( $order_id, $refund->get_amount(), $refund->get_reason() ); 
  1414.  
  1415. if ( is_wp_error( $result ) ) { 
  1416. return $result; 
  1417. } elseif ( ! $result ) { 
  1418. throw new WC_API_Exception( 'woocommerce_api_create_order_refund_api_failed', __( 'An error occurred while attempting to create the refund using the payment gateway API.', 'woocommerce' ), 500 ); 
  1419.  
  1420. // HTTP 201 Created 
  1421. $this->server->send_status( 201 ); 
  1422.  
  1423. do_action( 'woocommerce_api_create_order_refund', $refund->id, $order_id, $this ); 
  1424.  
  1425. return $this->get_order_refund( $order_id, $refund->id ); 
  1426. } catch ( WC_Data_Exception $e ) { 
  1427. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1428. } catch ( WC_API_Exception $e ) { 
  1429. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1430.  
  1431. /** 
  1432. * Edit an order refund 
  1433. * 
  1434. * @since 2.2 
  1435. * @param string $order_id order ID 
  1436. * @param string $id refund ID 
  1437. * @param array $data parsed request data 
  1438. * @return WP_Error|array error or edited refund response data 
  1439. */ 
  1440. public function edit_order_refund( $order_id, $id, $data ) { 
  1441. try { 
  1442. if ( ! isset( $data['order_refund'] ) ) { 
  1443. throw new WC_API_Exception( 'woocommerce_api_missing_order_refund_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'order_refund' ), 400 ); 
  1444.  
  1445. $data = $data['order_refund']; 
  1446.  
  1447. // Validate order ID 
  1448. $order_id = $this->validate_request( $order_id, $this->post_type, 'edit' ); 
  1449.  
  1450. if ( is_wp_error( $order_id ) ) { 
  1451. return $order_id; 
  1452.  
  1453. // Validate refund ID 
  1454. $id = absint( $id ); 
  1455.  
  1456. if ( empty( $id ) ) { 
  1457. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 ); 
  1458.  
  1459. // Ensure order ID is valid 
  1460. $refund = get_post( $id ); 
  1461.  
  1462. if ( ! $refund ) { 
  1463. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found.', 'woocommerce' ), 404 ); 
  1464.  
  1465. // Ensure refund ID is associated with given order 
  1466. if ( $refund->post_parent != $order_id ) { 
  1467. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order.', 'woocommerce' ), 400 ); 
  1468.  
  1469. $data = apply_filters( 'woocommerce_api_edit_order_refund_data', $data, $refund->ID, $order_id, $this ); 
  1470.  
  1471. // Update reason 
  1472. if ( isset( $data['reason'] ) ) { 
  1473. $updated_refund = wp_update_post( array( 'ID' => $refund->ID, 'post_excerpt' => $data['reason'] ) ); 
  1474.  
  1475. if ( is_wp_error( $updated_refund ) ) { 
  1476. return $updated_refund; 
  1477.  
  1478. // Update refund amount 
  1479. if ( isset( $data['amount'] ) && 0 < $data['amount'] ) { 
  1480. update_post_meta( $refund->ID, '_refund_amount', wc_format_decimal( $data['amount'] ) ); 
  1481.  
  1482. do_action( 'woocommerce_api_edit_order_refund', $refund->ID, $order_id, $this ); 
  1483.  
  1484. return $this->get_order_refund( $order_id, $refund->ID ); 
  1485. } catch ( WC_Data_Exception $e ) { 
  1486. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1487. } catch ( WC_API_Exception $e ) { 
  1488. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1489.  
  1490. /** 
  1491. * Delete order refund 
  1492. * 
  1493. * @since 2.2 
  1494. * @param string $order_id order ID 
  1495. * @param string $id refund ID 
  1496. * @return WP_Error|array error or deleted message 
  1497. */ 
  1498. public function delete_order_refund( $order_id, $id ) { 
  1499. try { 
  1500. $order_id = $this->validate_request( $order_id, $this->post_type, 'delete' ); 
  1501.  
  1502. if ( is_wp_error( $order_id ) ) { 
  1503. return $order_id; 
  1504.  
  1505. // Validate refund ID 
  1506. $id = absint( $id ); 
  1507.  
  1508. if ( empty( $id ) ) { 
  1509. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 ); 
  1510.  
  1511. // Ensure refund ID is valid 
  1512. $refund = get_post( $id ); 
  1513.  
  1514. if ( ! $refund ) { 
  1515. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found.', 'woocommerce' ), 404 ); 
  1516.  
  1517. // Ensure refund ID is associated with given order 
  1518. if ( $refund->post_parent != $order_id ) { 
  1519. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order.', 'woocommerce' ), 400 ); 
  1520.  
  1521. wc_delete_shop_order_transients( $order_id ); 
  1522.  
  1523. do_action( 'woocommerce_api_delete_order_refund', $refund->ID, $order_id, $this ); 
  1524.  
  1525. return $this->delete( $refund->ID, 'refund', true ); 
  1526. } catch ( WC_API_Exception $e ) { 
  1527. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1528.  
  1529. /** 
  1530. * Bulk update or insert orders 
  1531. * Accepts an array with orders in the formats supported by 
  1532. * WC_API_Orders->create_order() and WC_API_Orders->edit_order() 
  1533. * 
  1534. * @since 2.4.0 
  1535. * @param array $data 
  1536. * @return array 
  1537. */ 
  1538. public function bulk( $data ) { 
  1539.  
  1540. try { 
  1541. if ( ! isset( $data['orders'] ) ) { 
  1542. throw new WC_API_Exception( 'woocommerce_api_missing_orders_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'orders' ), 400 ); 
  1543.  
  1544. $data = $data['orders']; 
  1545. $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'orders' ); 
  1546.  
  1547. // Limit bulk operation 
  1548. if ( count( $data ) > $limit ) { 
  1549. throw new WC_API_Exception( 'woocommerce_api_orders_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'woocommerce' ), $limit ), 413 ); 
  1550.  
  1551. $orders = array(); 
  1552.  
  1553. foreach ( $data as $_order ) { 
  1554. $order_id = 0; 
  1555.  
  1556. // Try to get the order ID 
  1557. if ( isset( $_order['id'] ) ) { 
  1558. $order_id = intval( $_order['id'] ); 
  1559.  
  1560. // Order exists / edit order 
  1561. if ( $order_id ) { 
  1562. $edit = $this->edit_order( $order_id, array( 'order' => $_order ) ); 
  1563.  
  1564. if ( is_wp_error( $edit ) ) { 
  1565. $orders[] = array( 
  1566. 'id' => $order_id,  
  1567. 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),  
  1568. ); 
  1569. } else { 
  1570. $orders[] = $edit['order']; 
  1571. } else { 
  1572. // Order don't exists / create order 
  1573. $new = $this->create_order( array( 'order' => $_order ) ); 
  1574.  
  1575. if ( is_wp_error( $new ) ) { 
  1576. $orders[] = array( 
  1577. 'id' => $order_id,  
  1578. 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),  
  1579. ); 
  1580. } else { 
  1581. $orders[] = $new['order']; 
  1582.  
  1583. return array( 'orders' => apply_filters( 'woocommerce_api_orders_bulk_response', $orders, $this ) ); 
  1584. } catch ( WC_Data_Exception $e ) { 
  1585. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1586. } catch ( WC_API_Exception $e ) { 
  1587. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
.