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