/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. $order->update_status( $data['status'], isset( $data['status_note'] ) ? $data['status_note'] : '', true ); 
  561.  
  562. wc_delete_shop_order_transients( $order ); 
  563.  
  564. do_action( 'woocommerce_api_edit_order', $order->get_id(), $data, $this ); 
  565.  
  566. return $this->get_order( $id ); 
  567.  
  568. } catch ( WC_Data_Exception $e ) { 
  569. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  570. } catch ( WC_API_Exception $e ) { 
  571. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  572.  
  573. /** 
  574. * Delete an order 
  575. * 
  576. * @param int $id the order ID 
  577. * @param bool $force true to permanently delete order, false to move to trash 
  578. * @return array 
  579. */ 
  580. public function delete_order( $id, $force = false ) { 
  581.  
  582. $id = $this->validate_request( $id, $this->post_type, 'delete' ); 
  583.  
  584. if ( is_wp_error( $id ) ) { 
  585. return $id; 
  586.  
  587. wc_delete_shop_order_transients( $id ); 
  588.  
  589. do_action( 'woocommerce_api_delete_order', $id, $this ); 
  590.  
  591. return $this->delete( $id, 'order', ( 'true' === $force ) ); 
  592.  
  593. /** 
  594. * Helper method to get order post objects 
  595. * 
  596. * @since 2.1 
  597. * @param array $args request arguments for filtering query 
  598. * @return WP_Query 
  599. */ 
  600. protected function query_orders( $args ) { 
  601.  
  602. // set base query arguments 
  603. $query_args = array( 
  604. 'fields' => 'ids',  
  605. 'post_type' => $this->post_type,  
  606. 'post_status' => array_keys( wc_get_order_statuses() ),  
  607. ); 
  608.  
  609. // add status argument 
  610. if ( ! empty( $args['status'] ) ) { 
  611.  
  612. $statuses = 'wc-' . str_replace( ', ', ', wc-', $args['status'] ); 
  613. $statuses = explode( ', ', $statuses ); 
  614. $query_args['post_status'] = $statuses; 
  615.  
  616. unset( $args['status'] ); 
  617.  
  618.  
  619. $query_args = $this->merge_query_args( $query_args, $args ); 
  620.  
  621. return new WP_Query( $query_args ); 
  622.  
  623. /** 
  624. * Helper method to set/update the billing & shipping addresses for 
  625. * an order 
  626. * 
  627. * @since 2.1 
  628. * @param \WC_Order $order 
  629. * @param array $data 
  630. */ 
  631. protected function set_order_addresses( $order, $data ) { 
  632.  
  633. $address_fields = array( 
  634. 'first_name',  
  635. 'last_name',  
  636. 'company',  
  637. 'email',  
  638. 'phone',  
  639. 'address_1',  
  640. 'address_2',  
  641. 'city',  
  642. 'state',  
  643. 'postcode',  
  644. 'country',  
  645. ); 
  646.  
  647. $billing_address = $shipping_address = array(); 
  648.  
  649. // billing address 
  650. if ( isset( $data['billing_address'] ) && is_array( $data['billing_address'] ) ) { 
  651.  
  652. foreach ( $address_fields as $field ) { 
  653.  
  654. if ( isset( $data['billing_address'][ $field ] ) ) { 
  655. $billing_address[ $field ] = wc_clean( $data['billing_address'][ $field ] ); 
  656.  
  657. unset( $address_fields['email'] ); 
  658. unset( $address_fields['phone'] ); 
  659.  
  660. // shipping address 
  661. if ( isset( $data['shipping_address'] ) && is_array( $data['shipping_address'] ) ) { 
  662.  
  663. foreach ( $address_fields as $field ) { 
  664.  
  665. if ( isset( $data['shipping_address'][ $field ] ) ) { 
  666. $shipping_address[ $field ] = wc_clean( $data['shipping_address'][ $field ] ); 
  667.  
  668. $this->update_address( $order, $billing_address, 'billing' ); 
  669. $this->update_address( $order, $shipping_address, 'shipping' ); 
  670.  
  671. // update user meta 
  672. if ( $order->get_user_id() ) { 
  673. foreach ( $billing_address as $key => $value ) { 
  674. update_user_meta( $order->get_user_id(), 'billing_' . $key, $value ); 
  675. foreach ( $shipping_address as $key => $value ) { 
  676. update_user_meta( $order->get_user_id(), 'shipping_' . $key, $value ); 
  677.  
  678. /** 
  679. * Update address. 
  680. * 
  681. * @param WC_Order $order 
  682. * @param array $posted 
  683. * @param string $type 
  684. */ 
  685. protected function update_address( $order, $posted, $type = 'billing' ) { 
  686. foreach ( $posted as $key => $value ) { 
  687. if ( is_callable( array( $order, "set_{$type}_{$key}" ) ) ) { 
  688. $order->{"set_{$type}_{$key}"}( $value ); 
  689.  
  690. /** 
  691. * Helper method to add/update order meta, with two restrictions: 
  692. * 
  693. * 1) Only non-protected meta (no leading underscore) can be set 
  694. * 2) Meta values must be scalar (int, string, bool) 
  695. * 
  696. * @since 2.2 
  697. * @param int $order_id valid order ID 
  698. * @param array $order_meta order meta in array( 'meta_key' => 'meta_value' ) format 
  699. */ 
  700. protected function set_order_meta( $order_id, $order_meta ) { 
  701.  
  702. foreach ( $order_meta as $meta_key => $meta_value ) { 
  703.  
  704. if ( is_string( $meta_key ) && ! is_protected_meta( $meta_key ) && is_scalar( $meta_value ) ) { 
  705. update_post_meta( $order_id, $meta_key, $meta_value ); 
  706.  
  707. /** 
  708. * Helper method to check if the resource ID associated with the provided item is null 
  709. * 
  710. * Items can be deleted by setting the resource ID to null 
  711. * 
  712. * @since 2.2 
  713. * @param array $item item provided in the request body 
  714. * @return bool true if the item resource ID is null, false otherwise 
  715. */ 
  716. protected function item_is_null( $item ) { 
  717.  
  718. $keys = array( 'product_id', 'method_id', 'title', '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. * Wrapper method to create/update order items 
  728. * 
  729. * When updating, the item ID provided is checked to ensure it is associated 
  730. * with the order. 
  731. * 
  732. * @since 2.2 
  733. * @param \WC_Order $order order 
  734. * @param string $item_type 
  735. * @param array $item item provided in the request body 
  736. * @param string $action either 'create' or 'update' 
  737. * @throws WC_API_Exception if item ID is not associated with order 
  738. */ 
  739. protected function set_item( $order, $item_type, $item, $action ) { 
  740. global $wpdb; 
  741.  
  742. $set_method = "set_{$item_type}"; 
  743.  
  744. // verify provided line item ID is associated with order 
  745. if ( 'update' === $action ) { 
  746.  
  747. $result = $wpdb->get_row( 
  748. $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",  
  749. absint( $item['id'] ),  
  750. absint( $order->get_id() ) 
  751. ) ); 
  752.  
  753. if ( is_null( $result ) ) { 
  754. throw new WC_API_Exception( 'woocommerce_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 ); 
  755.  
  756. $this->$set_method( $order, $item, $action ); 
  757.  
  758. /** 
  759. * Create or update a line item 
  760. * 
  761. * @since 2.2 
  762. * @param \WC_Order $order 
  763. * @param array $item line item data 
  764. * @param string $action 'create' to add line item or 'update' to update it 
  765. * @throws WC_API_Exception invalid data, server error 
  766. */ 
  767. protected function set_line_item( $order, $item, $action ) { 
  768. $creating = ( 'create' === $action ); 
  769.  
  770. // product is always required 
  771. if ( ! isset( $item['product_id'] ) && ! isset( $item['sku'] ) ) { 
  772. throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID or SKU is required', 'woocommerce' ), 400 ); 
  773.  
  774. // when updating, ensure product ID provided matches 
  775. if ( 'update' === $action ) { 
  776.  
  777. $item_product_id = wc_get_order_item_meta( $item['id'], '_product_id' ); 
  778. $item_variation_id = wc_get_order_item_meta( $item['id'], '_variation_id' ); 
  779.  
  780. if ( $item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id ) { 
  781. throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID provided does not match this line item', 'woocommerce' ), 400 ); 
  782.  
  783. if ( isset( $item['product_id'] ) ) { 
  784. $product_id = $item['product_id']; 
  785. } elseif ( isset( $item['sku'] ) ) { 
  786. $product_id = wc_get_product_id_by_sku( $item['sku'] ); 
  787.  
  788. // variations must each have a key & value 
  789. $variation_id = 0; 
  790. if ( isset( $item['variations'] ) && is_array( $item['variations'] ) ) { 
  791. foreach ( $item['variations'] as $key => $value ) { 
  792. if ( ! $key || ! $value ) { 
  793. throw new WC_API_Exception( 'woocommerce_api_invalid_product_variation', __( 'The product variation is invalid', 'woocommerce' ), 400 ); 
  794. $variation_id = $this->get_variation_id( wc_get_product( $product_id ), $item['variations'] ); 
  795.  
  796. $product = wc_get_product( $variation_id ? $variation_id : $product_id ); 
  797.  
  798. // must be a valid WC_Product 
  799. if ( ! is_object( $product ) ) { 
  800. throw new WC_API_Exception( 'woocommerce_api_invalid_product', __( 'Product is invalid.', 'woocommerce' ), 400 ); 
  801.  
  802. // quantity must be positive float 
  803. if ( isset( $item['quantity'] ) && floatval( $item['quantity'] ) <= 0 ) { 
  804. throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity must be a positive float.', 'woocommerce' ), 400 ); 
  805.  
  806. // quantity is required when creating 
  807. if ( $creating && ! isset( $item['quantity'] ) ) { 
  808. throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity is required.', 'woocommerce' ), 400 ); 
  809.  
  810. if ( $creating ) { 
  811. $line_item = new WC_Order_Item_Product(); 
  812. } else { 
  813. $line_item = new WC_Order_Item_Product( $item['id'] ); 
  814.  
  815. $line_item->set_product( $product ); 
  816. $line_item->set_order_id( $order->get_id() ); 
  817.  
  818. if ( isset( $item['quantity'] ) ) { 
  819. $line_item->set_quantity( $item['quantity'] ); 
  820. if ( isset( $item['total'] ) ) { 
  821. $line_item->set_total( floatval( $item['total'] ) ); 
  822. } elseif ( $creating ) { 
  823. $total = wc_get_price_excluding_tax( $product, array( 'qty' => $line_item->get_quantity() ) ); 
  824. $line_item->set_total( $total ); 
  825. $line_item->set_subtotal( $total ); 
  826. if ( isset( $item['total_tax'] ) ) { 
  827. $line_item->set_total_tax( floatval( $item['total_tax'] ) ); 
  828. if ( isset( $item['subtotal'] ) ) { 
  829. $line_item->set_subtotal( floatval( $item['subtotal'] ) ); 
  830. if ( isset( $item['subtotal_tax'] ) ) { 
  831. $line_item->set_subtotal_tax( floatval( $item['subtotal_tax'] ) ); 
  832. if ( $variation_id ) { 
  833. $line_item->set_variation_id( $variation_id ); 
  834. $line_item->set_variation( $item['variations'] ); 
  835.  
  836. $item_id = $line_item->save(); 
  837.  
  838. if ( ! $item_id ) { 
  839. throw new WC_API_Exception( 'woocommerce_cannot_create_line_item', __( 'Cannot create line item, try again.', 'woocommerce' ), 500 ); 
  840.  
  841. /** 
  842. * Given a product ID & API provided variations, find the correct variation ID to use for calculation 
  843. * We can't just trust input from the API to pass a variation_id manually, otherwise you could pass 
  844. * the cheapest variation ID but provide other information so we have to look up the variation ID. 
  845. * @param int $product_id main product ID 
  846. * @return int returns an ID if a valid variation was found for this product 
  847. */ 
  848. function get_variation_id( $product, $variations = array() ) { 
  849. $variation_id = null; 
  850. $variations_normalized = array(); 
  851.  
  852. if ( $product->is_type( 'variable' ) && $product->has_child() ) { 
  853. if ( isset( $variations ) && is_array( $variations ) ) { 
  854. // start by normalizing the passed variations 
  855. foreach ( $variations as $key => $value ) { 
  856. $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); // from get_attributes in class-wc-api-products.php 
  857. $variations_normalized[ $key ] = strtolower( $value ); 
  858. // now search through each product child and see if our passed variations match anything 
  859. foreach ( $product->get_children() as $variation ) { 
  860. $meta = array(); 
  861. foreach ( get_post_meta( $variation ) as $key => $value ) { 
  862. $value = $value[0]; 
  863. $key = str_replace( 'attribute_', '', str_replace( 'pa_', '', $key ) ); 
  864. $meta[ $key ] = strtolower( $value ); 
  865. // if the variation array is a part of the $meta array, we found our match 
  866. if ( $this->array_contains( $variations_normalized, $meta ) ) { 
  867. $variation_id = $variation; 
  868. break; 
  869.  
  870. return $variation_id; 
  871.  
  872. /** 
  873. * Utility function to see if the meta array contains data from variations 
  874. */ 
  875. protected function array_contains( $needles, $haystack ) { 
  876. foreach ( $needles as $key => $value ) { 
  877. if ( $haystack[ $key ] !== $value ) { 
  878. return false; 
  879. return true; 
  880.  
  881. /** 
  882. * Create or update an order shipping method 
  883. * 
  884. * @since 2.2 
  885. * @param \WC_Order $order 
  886. * @param array $shipping item data 
  887. * @param string $action 'create' to add shipping or 'update' to update it 
  888. * @throws WC_API_Exception invalid data, server error 
  889. */ 
  890. protected function set_shipping( $order, $shipping, $action ) { 
  891.  
  892. // total must be a positive float 
  893. if ( isset( $shipping['total'] ) && floatval( $shipping['total'] ) < 0 ) { 
  894. throw new WC_API_Exception( 'woocommerce_invalid_shipping_total', __( 'Shipping total must be a positive amount.', 'woocommerce' ), 400 ); 
  895.  
  896. if ( 'create' === $action ) { 
  897.  
  898. // method ID is required 
  899. if ( ! isset( $shipping['method_id'] ) ) { 
  900. throw new WC_API_Exception( 'woocommerce_invalid_shipping_item', __( 'Shipping method ID is required.', 'woocommerce' ), 400 ); 
  901.  
  902. $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'] ); 
  903. $item = new WC_Order_Item_Shipping(); 
  904. $item->set_order_id( $order->get_id() ); 
  905. $item->set_shipping_rate( $rate ); 
  906. $shipping_id = $item->save(); 
  907.  
  908. if ( ! $shipping_id ) { 
  909. throw new WC_API_Exception( 'woocommerce_cannot_create_shipping', __( 'Cannot create shipping method, try again.', 'woocommerce' ), 500 ); 
  910. } else { 
  911.  
  912. $item = new WC_Order_Item_Shipping( $shipping['id'] ); 
  913.  
  914. if ( isset( $shipping['method_id'] ) ) { 
  915. $item->set_method_id( $shipping['method_id'] ); 
  916.  
  917. if ( isset( $shipping['method_title'] ) ) { 
  918. $item->set_method_title( $shipping['method_title'] ); 
  919.  
  920. if ( isset( $shipping['total'] ) ) { 
  921. $item->set_total( floatval( $shipping['total'] ) ); 
  922.  
  923. $shipping_id = $item->save(); 
  924.  
  925. if ( ! $shipping_id ) { 
  926. throw new WC_API_Exception( 'woocommerce_cannot_update_shipping', __( 'Cannot update shipping method, try again.', 'woocommerce' ), 500 ); 
  927.  
  928. /** 
  929. * Create or update an order fee 
  930. * 
  931. * @since 2.2 
  932. * @param \WC_Order $order 
  933. * @param array $fee item data 
  934. * @param string $action 'create' to add fee or 'update' to update it 
  935. * @throws WC_API_Exception invalid data, server error 
  936. */ 
  937. protected function set_fee( $order, $fee, $action ) { 
  938.  
  939. if ( 'create' === $action ) { 
  940.  
  941. // fee title is required 
  942. if ( ! isset( $fee['title'] ) ) { 
  943. throw new WC_API_Exception( 'woocommerce_invalid_fee_item', __( 'Fee title is required', 'woocommerce' ), 400 ); 
  944.  
  945. $item = new WC_Order_Item_Fee(); 
  946. $item->set_order_id( $order->get_id() ); 
  947. $item->set_name( wc_clean( $fee['title'] ) ); 
  948. $item->set_total( isset( $fee['total'] ) ? floatval( $fee['total'] ) : 0 ); 
  949.  
  950. // if taxable, tax class and total are required 
  951. if ( ! empty( $fee['taxable'] ) ) { 
  952. if ( ! isset( $fee['tax_class'] ) ) { 
  953. throw new WC_API_Exception( 'woocommerce_invalid_fee_item', __( 'Fee tax class is required when fee is taxable.', 'woocommerce' ), 400 ); 
  954.  
  955. $item->set_tax_status( 'taxable' ); 
  956. $item->set_tax_class( $fee['tax_class'] ); 
  957.  
  958. if ( isset( $fee['total_tax'] ) ) { 
  959. $item->set_total_tax( isset( $fee['total_tax'] ) ? wc_format_refund_total( $fee['total_tax'] ) : 0 ); 
  960.  
  961. if ( isset( $fee['tax_data'] ) ) { 
  962. $item->set_total_tax( wc_format_refund_total( array_sum( $fee['tax_data'] ) ) ); 
  963. $item->set_taxes( array_map( 'wc_format_refund_total', $fee['tax_data'] ) ); 
  964.  
  965. $fee_id = $item->save(); 
  966.  
  967. if ( ! $fee_id ) { 
  968. throw new WC_API_Exception( 'woocommerce_cannot_create_fee', __( 'Cannot create fee, try again.', 'woocommerce' ), 500 ); 
  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. $coupon_id = $item->save(); 
  1019.  
  1020. if ( ! $coupon_id ) { 
  1021. throw new WC_API_Exception( 'woocommerce_cannot_create_order_coupon', __( 'Cannot create coupon, try again.', 'woocommerce' ), 500 ); 
  1022. } else { 
  1023.  
  1024. $item = new WC_Order_Item_Coupon( $coupon['id'] ); 
  1025.  
  1026. if ( isset( $coupon['code'] ) ) { 
  1027. $item->set_code( $coupon['code'] ); 
  1028.  
  1029. if ( isset( $coupon['amount'] ) ) { 
  1030. $item->set_discount( floatval( $coupon['amount'] ) ); 
  1031.  
  1032. $coupon_id = $item->save(); 
  1033.  
  1034. if ( ! $coupon_id ) { 
  1035. throw new WC_API_Exception( 'woocommerce_cannot_update_order_coupon', __( 'Cannot update coupon, try again.', 'woocommerce' ), 500 ); 
  1036.  
  1037. /** 
  1038. * Get the admin order notes for an order 
  1039. * 
  1040. * @since 2.1 
  1041. * @param string $order_id order ID 
  1042. * @param string|null $fields fields to include in response 
  1043. * @return array 
  1044. */ 
  1045. public function get_order_notes( $order_id, $fields = null ) { 
  1046.  
  1047. // ensure ID is valid order ID 
  1048. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1049.  
  1050. if ( is_wp_error( $order_id ) ) { 
  1051. return $order_id; 
  1052.  
  1053. $args = array( 
  1054. 'post_id' => $order_id,  
  1055. 'approve' => 'approve',  
  1056. 'type' => 'order_note',  
  1057. ); 
  1058.  
  1059. remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); 
  1060.  
  1061. $notes = get_comments( $args ); 
  1062.  
  1063. add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); 
  1064.  
  1065. $order_notes = array(); 
  1066.  
  1067. foreach ( $notes as $note ) { 
  1068.  
  1069. $order_notes[] = current( $this->get_order_note( $order_id, $note->comment_ID, $fields ) ); 
  1070.  
  1071. return array( 'order_notes' => apply_filters( 'woocommerce_api_order_notes_response', $order_notes, $order_id, $fields, $notes, $this->server ) ); 
  1072.  
  1073. /** 
  1074. * Get an order note for the given order ID and ID 
  1075. * 
  1076. * @since 2.2 
  1077. * @param string $order_id order ID 
  1078. * @param string $id order note ID 
  1079. * @param string|null $fields fields to limit response to 
  1080. * @return array 
  1081. */ 
  1082. public function get_order_note( $order_id, $id, $fields = null ) { 
  1083. try { 
  1084. // Validate order ID 
  1085. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1086.  
  1087. if ( is_wp_error( $order_id ) ) { 
  1088. return $order_id; 
  1089.  
  1090. $id = absint( $id ); 
  1091.  
  1092. if ( empty( $id ) ) { 
  1093. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 ); 
  1094.  
  1095. $note = get_comment( $id ); 
  1096.  
  1097. if ( is_null( $note ) ) { 
  1098. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 ); 
  1099.  
  1100. $order_note = array( 
  1101. 'id' => $note->comment_ID,  
  1102. 'created_at' => $this->server->format_datetime( $note->comment_date_gmt ),  
  1103. 'note' => $note->comment_content,  
  1104. 'customer_note' => get_comment_meta( $note->comment_ID, 'is_customer_note', true ) ? true : false,  
  1105. ); 
  1106.  
  1107. return array( 'order_note' => apply_filters( 'woocommerce_api_order_note_response', $order_note, $id, $fields, $note, $order_id, $this ) ); 
  1108. } catch ( WC_API_Exception $e ) { 
  1109. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1110.  
  1111. /** 
  1112. * Create a new order note for the given order 
  1113. * 
  1114. * @since 2.2 
  1115. * @param string $order_id order ID 
  1116. * @param array $data raw request data 
  1117. * @return WP_Error|array error or created note response data 
  1118. */ 
  1119. public function create_order_note( $order_id, $data ) { 
  1120. try { 
  1121. if ( ! isset( $data['order_note'] ) ) { 
  1122. 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 ); 
  1123.  
  1124. $data = $data['order_note']; 
  1125.  
  1126. // permission check 
  1127. if ( ! current_user_can( 'publish_shop_orders' ) ) { 
  1128. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_note', __( 'You do not have permission to create order notes', 'woocommerce' ), 401 ); 
  1129.  
  1130. $order_id = $this->validate_request( $order_id, $this->post_type, 'edit' ); 
  1131.  
  1132. if ( is_wp_error( $order_id ) ) { 
  1133. return $order_id; 
  1134.  
  1135. $order = wc_get_order( $order_id ); 
  1136.  
  1137. $data = apply_filters( 'woocommerce_api_create_order_note_data', $data, $order_id, $this ); 
  1138.  
  1139. // note content is required 
  1140. if ( ! isset( $data['note'] ) ) { 
  1141. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note', __( 'Order note is required', 'woocommerce' ), 400 ); 
  1142.  
  1143. $is_customer_note = ( isset( $data['customer_note'] ) && true === $data['customer_note'] ); 
  1144.  
  1145. // create the note 
  1146. $note_id = $order->add_order_note( $data['note'], $is_customer_note ); 
  1147.  
  1148. if ( ! $note_id ) { 
  1149. throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again.', 'woocommerce' ), 500 ); 
  1150.  
  1151. // HTTP 201 Created 
  1152. $this->server->send_status( 201 ); 
  1153.  
  1154. do_action( 'woocommerce_api_create_order_note', $note_id, $order_id, $this ); 
  1155.  
  1156. return $this->get_order_note( $order->get_id(), $note_id ); 
  1157. } catch ( WC_Data_Exception $e ) { 
  1158. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1159. } catch ( WC_API_Exception $e ) { 
  1160. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1161.  
  1162. /** 
  1163. * Edit the order note 
  1164. * 
  1165. * @since 2.2 
  1166. * @param string $order_id order ID 
  1167. * @param string $id note ID 
  1168. * @param array $data parsed request data 
  1169. * @return WP_Error|array error or edited note response data 
  1170. */ 
  1171. public function edit_order_note( $order_id, $id, $data ) { 
  1172. try { 
  1173. if ( ! isset( $data['order_note'] ) ) { 
  1174. 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 ); 
  1175.  
  1176. $data = $data['order_note']; 
  1177.  
  1178. // Validate order ID 
  1179. $order_id = $this->validate_request( $order_id, $this->post_type, 'edit' ); 
  1180.  
  1181. if ( is_wp_error( $order_id ) ) { 
  1182. return $order_id; 
  1183.  
  1184. $order = wc_get_order( $order_id ); 
  1185.  
  1186. // Validate note ID 
  1187. $id = absint( $id ); 
  1188.  
  1189. if ( empty( $id ) ) { 
  1190. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 ); 
  1191.  
  1192. // Ensure note ID is valid 
  1193. $note = get_comment( $id ); 
  1194.  
  1195. if ( is_null( $note ) ) { 
  1196. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 ); 
  1197.  
  1198. // Ensure note ID is associated with given order 
  1199. if ( $note->comment_post_ID != $order->get_id() ) { 
  1200. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), 400 ); 
  1201.  
  1202. $data = apply_filters( 'woocommerce_api_edit_order_note_data', $data, $note->comment_ID, $order->get_id(), $this ); 
  1203.  
  1204. // Note content 
  1205. if ( isset( $data['note'] ) ) { 
  1206.  
  1207. wp_update_comment( 
  1208. array( 
  1209. 'comment_ID' => $note->comment_ID,  
  1210. 'comment_content' => $data['note'],  
  1211. ); 
  1212.  
  1213. // Customer note 
  1214. if ( isset( $data['customer_note'] ) ) { 
  1215.  
  1216. update_comment_meta( $note->comment_ID, 'is_customer_note', true === $data['customer_note'] ? 1 : 0 ); 
  1217.  
  1218. do_action( 'woocommerce_api_edit_order_note', $note->comment_ID, $order->get_id(), $this ); 
  1219.  
  1220. return $this->get_order_note( $order->get_id(), $note->comment_ID ); 
  1221. } catch ( WC_Data_Exception $e ) { 
  1222. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1223. } catch ( WC_API_Exception $e ) { 
  1224. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1225.  
  1226. /** 
  1227. * Delete order note 
  1228. * 
  1229. * @since 2.2 
  1230. * @param string $order_id order ID 
  1231. * @param string $id note ID 
  1232. * @return WP_Error|array error or deleted message 
  1233. */ 
  1234. public function delete_order_note( $order_id, $id ) { 
  1235. try { 
  1236. $order_id = $this->validate_request( $order_id, $this->post_type, 'delete' ); 
  1237.  
  1238. if ( is_wp_error( $order_id ) ) { 
  1239. return $order_id; 
  1240.  
  1241. // Validate note ID 
  1242. $id = absint( $id ); 
  1243.  
  1244. if ( empty( $id ) ) { 
  1245. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'Invalid order note ID', 'woocommerce' ), 400 ); 
  1246.  
  1247. // Ensure note ID is valid 
  1248. $note = get_comment( $id ); 
  1249.  
  1250. if ( is_null( $note ) ) { 
  1251. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'An order note with the provided ID could not be found', 'woocommerce' ), 404 ); 
  1252.  
  1253. // Ensure note ID is associated with given order 
  1254. if ( $note->comment_post_ID != $order_id ) { 
  1255. throw new WC_API_Exception( 'woocommerce_api_invalid_order_note_id', __( 'The order note ID provided is not associated with the order', 'woocommerce' ), 400 ); 
  1256.  
  1257. // Force delete since trashed order notes could not be managed through comments list table 
  1258. $result = wp_delete_comment( $note->comment_ID, true ); 
  1259.  
  1260. if ( ! $result ) { 
  1261. throw new WC_API_Exception( 'woocommerce_api_cannot_delete_order_note', __( 'This order note cannot be deleted', 'woocommerce' ), 500 ); 
  1262.  
  1263. do_action( 'woocommerce_api_delete_order_note', $note->comment_ID, $order_id, $this ); 
  1264.  
  1265. return array( 'message' => __( 'Permanently deleted order note', 'woocommerce' ) ); 
  1266. } catch ( WC_API_Exception $e ) { 
  1267. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1268.  
  1269. /** 
  1270. * Get the order refunds for an order 
  1271. * 
  1272. * @since 2.2 
  1273. * @param string $order_id order ID 
  1274. * @param string|null $fields fields to include in response 
  1275. * @return array 
  1276. */ 
  1277. public function get_order_refunds( $order_id, $fields = null ) { 
  1278.  
  1279. // Ensure ID is valid order ID 
  1280. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1281.  
  1282. if ( is_wp_error( $order_id ) ) { 
  1283. return $order_id; 
  1284.  
  1285. $refund_items = wc_get_orders( array( 
  1286. 'type' => 'shop_order_refund',  
  1287. 'parent' => $order_id,  
  1288. 'limit' => -1,  
  1289. 'return' => 'ids',  
  1290. ) ); 
  1291. $order_refunds = array(); 
  1292.  
  1293. foreach ( $refund_items as $refund_id ) { 
  1294. $order_refunds[] = current( $this->get_order_refund( $order_id, $refund_id, $fields ) ); 
  1295.  
  1296. return array( 'order_refunds' => apply_filters( 'woocommerce_api_order_refunds_response', $order_refunds, $order_id, $fields, $refund_items, $this ) ); 
  1297.  
  1298. /** 
  1299. * Get an order refund for the given order ID and ID 
  1300. * 
  1301. * @since 2.2 
  1302. * @param string $order_id order ID 
  1303. * @param string|null $fields fields to limit response to 
  1304. * @return array 
  1305. */ 
  1306. public function get_order_refund( $order_id, $id, $fields = null ) { 
  1307. try { 
  1308. // Validate order ID 
  1309. $order_id = $this->validate_request( $order_id, $this->post_type, 'read' ); 
  1310.  
  1311. if ( is_wp_error( $order_id ) ) { 
  1312. return $order_id; 
  1313.  
  1314. $id = absint( $id ); 
  1315.  
  1316. if ( empty( $id ) ) { 
  1317. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 ); 
  1318.  
  1319. $order = wc_get_order( $order_id ); 
  1320. $refund = wc_get_order( $id ); 
  1321.  
  1322. if ( ! $refund ) { 
  1323. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found.', 'woocommerce' ), 404 ); 
  1324.  
  1325. $line_items = array(); 
  1326.  
  1327. // Add line items 
  1328. foreach ( $refund->get_items( 'line_item' ) as $item_id => $item ) { 
  1329. $product = $item->get_product(); 
  1330. $hideprefix = ( isset( $filter['all_item_meta'] ) && 'true' === $filter['all_item_meta'] ) ? null : '_'; 
  1331. $item_meta = $item->get_formatted_meta_data( $hideprefix ); 
  1332.  
  1333. foreach ( $item_meta as $key => $values ) { 
  1334. $item_meta[ $key ]->label = $values->display_key; 
  1335. unset( $item_meta[ $key ]->display_key ); 
  1336. unset( $item_meta[ $key ]->display_value ); 
  1337.  
  1338. $line_items[] = array( 
  1339. 'id' => $item_id,  
  1340. 'subtotal' => wc_format_decimal( $order->get_line_subtotal( $item ), 2 ),  
  1341. 'subtotal_tax' => wc_format_decimal( $item->get_subtotal_tax(), 2 ),  
  1342. 'total' => wc_format_decimal( $order->get_line_total( $item ), 2 ),  
  1343. 'total_tax' => wc_format_decimal( $order->get_line_tax( $item ), 2 ),  
  1344. 'price' => wc_format_decimal( $order->get_item_total( $item ), 2 ),  
  1345. 'quantity' => $item->get_quantity(),  
  1346. 'tax_class' => $item->get_tax_class(),  
  1347. 'name' => $item->get_name(),  
  1348. 'product_id' => $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(),  
  1349. 'sku' => is_object( $product ) ? $product->get_sku() : null,  
  1350. 'meta' => array_values( $item_meta ),  
  1351. 'refunded_item_id' => (int) $item->get_meta( 'refunded_item_id' ),  
  1352. ); 
  1353.  
  1354. $order_refund = array( 
  1355. 'id' => $refund->id,  
  1356. 'created_at' => $this->server->format_datetime( $refund->get_date_created() ? $refund->get_date_created()->getTimestamp() : 0, false, false ),  
  1357. 'amount' => wc_format_decimal( $refund->get_amount(), 2 ),  
  1358. 'reason' => $refund->get_reason(),  
  1359. 'line_items' => $line_items,  
  1360. ); 
  1361.  
  1362. return array( 'order_refund' => apply_filters( 'woocommerce_api_order_refund_response', $order_refund, $id, $fields, $refund, $order_id, $this ) ); 
  1363. } catch ( WC_API_Exception $e ) { 
  1364. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1365.  
  1366. /** 
  1367. * Create a new order refund for the given order 
  1368. * 
  1369. * @since 2.2 
  1370. * @param string $order_id order ID 
  1371. * @param array $data raw request data 
  1372. * @param bool $api_refund do refund using a payment gateway API 
  1373. * @return WP_Error|array error or created refund response data 
  1374. */ 
  1375. public function create_order_refund( $order_id, $data, $api_refund = true ) { 
  1376. try { 
  1377. if ( ! isset( $data['order_refund'] ) ) { 
  1378. 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 ); 
  1379.  
  1380. $data = $data['order_refund']; 
  1381.  
  1382. // Permission check 
  1383. if ( ! current_user_can( 'publish_shop_orders' ) ) { 
  1384. throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_refund', __( 'You do not have permission to create order refunds', 'woocommerce' ), 401 ); 
  1385.  
  1386. $order_id = absint( $order_id ); 
  1387.  
  1388. if ( empty( $order_id ) ) { 
  1389. throw new WC_API_Exception( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), 400 ); 
  1390.  
  1391. $data = apply_filters( 'woocommerce_api_create_order_refund_data', $data, $order_id, $this ); 
  1392.  
  1393. // Refund amount is required 
  1394. if ( ! isset( $data['amount'] ) ) { 
  1395. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount is required.', 'woocommerce' ), 400 ); 
  1396. } elseif ( 0 > $data['amount'] ) { 
  1397. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund', __( 'Refund amount must be positive.', 'woocommerce' ), 400 ); 
  1398.  
  1399. $data['order_id'] = $order_id; 
  1400. $data['refund_id'] = 0; 
  1401.  
  1402. // Create the refund 
  1403. $refund = wc_create_refund( $data ); 
  1404.  
  1405. if ( ! $refund ) { 
  1406. throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 ); 
  1407.  
  1408. // Refund via API 
  1409. if ( $api_refund ) { 
  1410. if ( WC()->payment_gateways() ) { 
  1411. $payment_gateways = WC()->payment_gateways->payment_gateways(); 
  1412.  
  1413. $order = wc_get_order( $order_id ); 
  1414.  
  1415. if ( isset( $payment_gateways[ $order->get_payment_method() ] ) && $payment_gateways[ $order->get_payment_method() ]->supports( 'refunds' ) ) { 
  1416. $result = $payment_gateways[ $order->get_payment_method() ]->process_refund( $order_id, $refund->get_amount(), $refund->get_reason() ); 
  1417.  
  1418. if ( is_wp_error( $result ) ) { 
  1419. return $result; 
  1420. } elseif ( ! $result ) { 
  1421. 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 ); 
  1422.  
  1423. // HTTP 201 Created 
  1424. $this->server->send_status( 201 ); 
  1425.  
  1426. do_action( 'woocommerce_api_create_order_refund', $refund->id, $order_id, $this ); 
  1427.  
  1428. return $this->get_order_refund( $order_id, $refund->id ); 
  1429. } catch ( WC_Data_Exception $e ) { 
  1430. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1431. } catch ( WC_API_Exception $e ) { 
  1432. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1433.  
  1434. /** 
  1435. * Edit an order refund 
  1436. * 
  1437. * @since 2.2 
  1438. * @param string $order_id order ID 
  1439. * @param string $id refund ID 
  1440. * @param array $data parsed request data 
  1441. * @return WP_Error|array error or edited refund response data 
  1442. */ 
  1443. public function edit_order_refund( $order_id, $id, $data ) { 
  1444. try { 
  1445. if ( ! isset( $data['order_refund'] ) ) { 
  1446. 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 ); 
  1447.  
  1448. $data = $data['order_refund']; 
  1449.  
  1450. // Validate order ID 
  1451. $order_id = $this->validate_request( $order_id, $this->post_type, 'edit' ); 
  1452.  
  1453. if ( is_wp_error( $order_id ) ) { 
  1454. return $order_id; 
  1455.  
  1456. // Validate refund ID 
  1457. $id = absint( $id ); 
  1458.  
  1459. if ( empty( $id ) ) { 
  1460. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 ); 
  1461.  
  1462. // Ensure order ID is valid 
  1463. $refund = get_post( $id ); 
  1464.  
  1465. if ( ! $refund ) { 
  1466. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found.', 'woocommerce' ), 404 ); 
  1467.  
  1468. // Ensure refund ID is associated with given order 
  1469. if ( $refund->post_parent != $order_id ) { 
  1470. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order.', 'woocommerce' ), 400 ); 
  1471.  
  1472. $data = apply_filters( 'woocommerce_api_edit_order_refund_data', $data, $refund->ID, $order_id, $this ); 
  1473.  
  1474. // Update reason 
  1475. if ( isset( $data['reason'] ) ) { 
  1476. $updated_refund = wp_update_post( array( 'ID' => $refund->ID, 'post_excerpt' => $data['reason'] ) ); 
  1477.  
  1478. if ( is_wp_error( $updated_refund ) ) { 
  1479. return $updated_refund; 
  1480.  
  1481. // Update refund amount 
  1482. if ( isset( $data['amount'] ) && 0 < $data['amount'] ) { 
  1483. update_post_meta( $refund->ID, '_refund_amount', wc_format_decimal( $data['amount'] ) ); 
  1484.  
  1485. do_action( 'woocommerce_api_edit_order_refund', $refund->ID, $order_id, $this ); 
  1486.  
  1487. return $this->get_order_refund( $order_id, $refund->ID ); 
  1488. } catch ( WC_Data_Exception $e ) { 
  1489. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1490. } catch ( WC_API_Exception $e ) { 
  1491. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1492.  
  1493. /** 
  1494. * Delete order refund 
  1495. * 
  1496. * @since 2.2 
  1497. * @param string $order_id order ID 
  1498. * @param string $id refund ID 
  1499. * @return WP_Error|array error or deleted message 
  1500. */ 
  1501. public function delete_order_refund( $order_id, $id ) { 
  1502. try { 
  1503. $order_id = $this->validate_request( $order_id, $this->post_type, 'delete' ); 
  1504.  
  1505. if ( is_wp_error( $order_id ) ) { 
  1506. return $order_id; 
  1507.  
  1508. // Validate refund ID 
  1509. $id = absint( $id ); 
  1510.  
  1511. if ( empty( $id ) ) { 
  1512. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 ); 
  1513.  
  1514. // Ensure refund ID is valid 
  1515. $refund = get_post( $id ); 
  1516.  
  1517. if ( ! $refund ) { 
  1518. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found.', 'woocommerce' ), 404 ); 
  1519.  
  1520. // Ensure refund ID is associated with given order 
  1521. if ( $refund->post_parent != $order_id ) { 
  1522. throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order.', 'woocommerce' ), 400 ); 
  1523.  
  1524. wc_delete_shop_order_transients( $order_id ); 
  1525.  
  1526. do_action( 'woocommerce_api_delete_order_refund', $refund->ID, $order_id, $this ); 
  1527.  
  1528. return $this->delete( $refund->ID, 'refund', true ); 
  1529. } catch ( WC_API_Exception $e ) { 
  1530. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
  1531.  
  1532. /** 
  1533. * Bulk update or insert orders 
  1534. * Accepts an array with orders in the formats supported by 
  1535. * WC_API_Orders->create_order() and WC_API_Orders->edit_order() 
  1536. * 
  1537. * @since 2.4.0 
  1538. * @param array $data 
  1539. * @return array 
  1540. */ 
  1541. public function bulk( $data ) { 
  1542.  
  1543. try { 
  1544. if ( ! isset( $data['orders'] ) ) { 
  1545. throw new WC_API_Exception( 'woocommerce_api_missing_orders_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'orders' ), 400 ); 
  1546.  
  1547. $data = $data['orders']; 
  1548. $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'orders' ); 
  1549.  
  1550. // Limit bulk operation 
  1551. if ( count( $data ) > $limit ) { 
  1552. 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 ); 
  1553.  
  1554. $orders = array(); 
  1555.  
  1556. foreach ( $data as $_order ) { 
  1557. $order_id = 0; 
  1558.  
  1559. // Try to get the order ID 
  1560. if ( isset( $_order['id'] ) ) { 
  1561. $order_id = intval( $_order['id'] ); 
  1562.  
  1563. // Order exists / edit order 
  1564. if ( $order_id ) { 
  1565. $edit = $this->edit_order( $order_id, array( 'order' => $_order ) ); 
  1566.  
  1567. if ( is_wp_error( $edit ) ) { 
  1568. $orders[] = array( 
  1569. 'id' => $order_id,  
  1570. 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ),  
  1571. ); 
  1572. } else { 
  1573. $orders[] = $edit['order']; 
  1574. } else { 
  1575. // Order don't exists / create order 
  1576. $new = $this->create_order( array( 'order' => $_order ) ); 
  1577.  
  1578. if ( is_wp_error( $new ) ) { 
  1579. $orders[] = array( 
  1580. 'id' => $order_id,  
  1581. 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ),  
  1582. ); 
  1583. } else { 
  1584. $orders[] = $new['order']; 
  1585.  
  1586. return array( 'orders' => apply_filters( 'woocommerce_api_orders_bulk_response', $orders, $this ) ); 
  1587. } catch ( WC_Data_Exception $e ) { 
  1588. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) ); 
  1589. } catch ( WC_API_Exception $e ) { 
  1590. return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); 
.