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