2014-07-12 21:49:43 +00:00
< ? php
/**
* WooCommerce API Orders Class
*
* Handles requests to the / orders endpoint
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.1
* @ version 2.1
*/
2014-09-20 19:27:54 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2014-07-12 21:49:43 +00:00
class WC_API_Orders extends WC_API_Resource {
/** @var string $base the route base */
protected $base = '/orders' ;
/**
* Register the routes for this class
*
* GET / orders
* GET / orders / count
* GET | PUT / orders /< id >
* GET / orders /< id >/ notes
*
* @ since 2.1
* @ param array $routes
* @ return array
*/
public function register_routes ( $routes ) {
# GET /orders
$routes [ $this -> base ] = array (
array ( array ( $this , 'get_orders' ), WC_API_Server :: READABLE ),
);
# GET /orders/count
2016-08-27 03:11:30 +00:00
$routes [ $this -> base . '/count' ] = array (
2014-07-12 21:49:43 +00:00
array ( array ( $this , 'get_orders_count' ), WC_API_Server :: READABLE ),
);
# GET|PUT /orders/<id>
$routes [ $this -> base . '/(?P<id>\d+)' ] = array (
array ( array ( $this , 'get_order' ), WC_API_Server :: READABLE ),
array ( array ( $this , 'edit_order' ), WC_API_Server :: EDITABLE | WC_API_Server :: ACCEPT_DATA ),
);
# GET /orders/<id>/notes
$routes [ $this -> base . '/(?P<id>\d+)/notes' ] = array (
array ( array ( $this , 'get_order_notes' ), WC_API_Server :: READABLE ),
);
return $routes ;
}
/**
* Get all orders
*
* @ since 2.1
* @ param string $fields
* @ param array $filter
* @ param string $status
* @ param int $page
* @ return array
*/
public function get_orders ( $fields = null , $filter = array (), $status = null , $page = 1 ) {
2017-03-07 20:24:24 +00:00
if ( ! empty ( $status ) ) {
2014-07-12 21:49:43 +00:00
$filter [ 'status' ] = $status ;
2017-03-07 20:24:24 +00:00
}
2014-07-12 21:49:43 +00:00
$filter [ 'page' ] = $page ;
$query = $this -> query_orders ( $filter );
$orders = array ();
2016-08-27 04:23:02 +00:00
foreach ( $query -> posts as $order_id ) {
2014-07-12 21:49:43 +00:00
2017-03-07 20:24:24 +00:00
if ( ! $this -> is_readable ( $order_id ) ) {
2014-07-12 21:49:43 +00:00
continue ;
2017-03-07 20:24:24 +00:00
}
2014-07-12 21:49:43 +00:00
$orders [] = current ( $this -> get_order ( $order_id , $fields ) );
}
$this -> server -> add_pagination_headers ( $query );
return array ( 'orders' => $orders );
}
/**
* Get the order for the given ID
*
* @ since 2.1
* @ param int $id the order ID
* @ param array $fields
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2014-07-12 21:49:43 +00:00
*/
public function get_order ( $id , $fields = null ) {
// ensure order ID is valid & user has permission to read
$id = $this -> validate_request ( $id , 'shop_order' , 'read' );
2016-11-18 19:06:28 +00:00
if ( is_wp_error ( $id ) ) {
2014-07-12 21:49:43 +00:00
return $id ;
2016-11-18 19:06:28 +00:00
}
2014-07-12 21:49:43 +00:00
2016-11-18 18:46:59 +00:00
$order = wc_get_order ( $id );
2014-07-12 21:49:43 +00:00
$order_data = array (
2016-08-05 14:56:23 +00:00
'id' => $order -> get_id (),
2014-07-12 21:49:43 +00:00
'order_number' => $order -> get_order_number (),
2017-03-10 16:28:50 +00:00
'created_at' => $this -> server -> format_datetime ( $order -> get_date_created () ? $order -> get_date_created () -> getTimestamp () : 0 , false , false ), // API gives UTC times.
'updated_at' => $this -> server -> format_datetime ( $order -> get_date_modified () ? $order -> get_date_modified () -> getTimestamp () : 0 , false , false ), // API gives UTC times.
'completed_at' => $this -> server -> format_datetime ( $order -> get_date_completed () ? $order -> get_date_completed () -> getTimestamp () : 0 , false , false ), // API gives UTC times.
2014-07-12 21:49:43 +00:00
'status' => $order -> get_status (),
2016-08-19 16:05:47 +00:00
'currency' => $order -> get_currency (),
2014-07-12 21:49:43 +00:00
'total' => wc_format_decimal ( $order -> get_total (), 2 ),
'subtotal' => wc_format_decimal ( $this -> get_order_subtotal ( $order ), 2 ),
'total_line_items_quantity' => $order -> get_item_count (),
'total_tax' => wc_format_decimal ( $order -> get_total_tax (), 2 ),
2016-08-22 15:57:54 +00:00
'total_shipping' => wc_format_decimal ( $order -> get_shipping_total (), 2 ),
2014-07-12 21:49:43 +00:00
'cart_tax' => wc_format_decimal ( $order -> get_cart_tax (), 2 ),
'shipping_tax' => wc_format_decimal ( $order -> get_shipping_tax (), 2 ),
'total_discount' => wc_format_decimal ( $order -> get_total_discount (), 2 ),
2016-11-18 19:06:28 +00:00
'cart_discount' => wc_format_decimal ( 0 , 2 ),
'order_discount' => wc_format_decimal ( 0 , 2 ),
2014-07-12 21:49:43 +00:00
'shipping_methods' => $order -> get_shipping_method (),
'payment_details' => array (
2016-08-05 15:04:41 +00:00
'method_id' => $order -> get_payment_method (),
2016-08-05 14:59:55 +00:00
'method_title' => $order -> get_payment_method_title (),
2017-03-10 16:28:50 +00:00
'paid' => ! is_null ( $order -> get_date_paid () ),
2014-07-12 21:49:43 +00:00
),
'billing_address' => array (
2016-08-05 14:58:44 +00:00
'first_name' => $order -> get_billing_first_name (),
2016-08-05 14:57:40 +00:00
'last_name' => $order -> get_billing_last_name (),
2016-08-05 15:03:09 +00:00
'company' => $order -> get_billing_company (),
2016-08-08 15:46:58 +00:00
'address_1' => $order -> get_billing_address_1 (),
'address_2' => $order -> get_billing_address_2 (),
'city' => $order -> get_billing_city (),
'state' => $order -> get_billing_state (),
'postcode' => $order -> get_billing_postcode (),
'country' => $order -> get_billing_country (),
2016-08-05 14:57:40 +00:00
'email' => $order -> get_billing_email (),
2016-08-05 15:09:04 +00:00
'phone' => $order -> get_billing_phone (),
2014-07-12 21:49:43 +00:00
),
'shipping_address' => array (
2016-08-08 15:46:58 +00:00
'first_name' => $order -> get_shipping_first_name (),
'last_name' => $order -> get_shipping_last_name (),
'company' => $order -> get_shipping_company (),
'address_1' => $order -> get_shipping_address_1 (),
'address_2' => $order -> get_shipping_address_2 (),
'city' => $order -> get_shipping_city (),
'state' => $order -> get_shipping_state (),
'postcode' => $order -> get_shipping_postcode (),
'country' => $order -> get_shipping_country (),
2014-07-12 21:49:43 +00:00
),
2016-08-08 15:24:16 +00:00
'note' => $order -> get_customer_note (),
'customer_ip' => $order -> get_customer_ip_address (),
2016-08-12 10:54:00 +00:00
'customer_user_agent' => $order -> get_customer_user_agent (),
2016-08-05 15:09:40 +00:00
'customer_id' => $order -> get_user_id (),
2014-07-12 21:49:43 +00:00
'view_order_url' => $order -> get_view_order_url (),
'line_items' => array (),
'shipping_lines' => array (),
'tax_lines' => array (),
'fee_lines' => array (),
'coupon_lines' => array (),
);
// add line items
2016-08-27 04:23:02 +00:00
foreach ( $order -> get_items () as $item_id => $item ) {
2016-08-19 16:05:47 +00:00
$product = $item -> get_product ();
2014-07-12 21:49:43 +00:00
$order_data [ 'line_items' ][] = array (
'id' => $item_id ,
'subtotal' => wc_format_decimal ( $order -> get_line_subtotal ( $item ), 2 ),
'total' => wc_format_decimal ( $order -> get_line_total ( $item ), 2 ),
'total_tax' => wc_format_decimal ( $order -> get_line_tax ( $item ), 2 ),
'price' => wc_format_decimal ( $order -> get_item_total ( $item ), 2 ),
2016-08-19 16:05:47 +00:00
'quantity' => $item -> get_quantity (),
'tax_class' => $item -> get_tax_class (),
2016-08-19 14:08:00 +00:00
'name' => $item -> get_name (),
2016-08-19 16:05:47 +00:00
'product_id' => $item -> get_variation_id () ? $item -> get_variation_id () : $item -> get_product_id (),
2014-07-12 21:49:43 +00:00
'sku' => is_object ( $product ) ? $product -> get_sku () : null ,
);
}
// add shipping
foreach ( $order -> get_shipping_methods () as $shipping_item_id => $shipping_item ) {
$order_data [ 'shipping_lines' ][] = array (
'id' => $shipping_item_id ,
2016-08-19 16:05:47 +00:00
'method_id' => $shipping_item -> get_method_id (),
'method_title' => $shipping_item -> get_name (),
'total' => wc_format_decimal ( $shipping_item -> get_total (), 2 ),
2014-07-12 21:49:43 +00:00
);
}
// add taxes
foreach ( $order -> get_tax_totals () as $tax_code => $tax ) {
$order_data [ 'tax_lines' ][] = array (
'code' => $tax_code ,
'title' => $tax -> label ,
'total' => wc_format_decimal ( $tax -> amount , 2 ),
'compound' => ( bool ) $tax -> is_compound ,
);
}
// add fees
foreach ( $order -> get_fees () as $fee_item_id => $fee_item ) {
$order_data [ 'fee_lines' ][] = array (
'id' => $fee_item_id ,
2016-08-19 16:05:47 +00:00
'title' => $fee_item -> get_name (),
'tax_class' => $fee_item -> get_tax_class (),
2014-07-12 21:49:43 +00:00
'total' => wc_format_decimal ( $order -> get_line_total ( $fee_item ), 2 ),
'total_tax' => wc_format_decimal ( $order -> get_line_tax ( $fee_item ), 2 ),
);
}
// add coupons
foreach ( $order -> get_items ( 'coupon' ) as $coupon_item_id => $coupon_item ) {
$order_data [ 'coupon_lines' ][] = array (
'id' => $coupon_item_id ,
2016-08-19 16:05:47 +00:00
'code' => $coupon_item -> get_code (),
2017-03-17 14:37:19 +00:00
'amount' => wc_format_decimal ( $coupon_item -> get_discount (), 2 ),
2014-07-12 21:49:43 +00:00
);
}
return array ( 'order' => apply_filters ( 'woocommerce_api_order_response' , $order_data , $order , $fields , $this -> server ) );
}
/**
* Get the total number of orders
*
* @ since 2.1
2017-05-15 11:50:52 +00:00
*
2014-07-12 21:49:43 +00:00
* @ param string $status
* @ param array $filter
2017-05-15 11:50:52 +00:00
*
* @ return array | WP_Error
2014-07-12 21:49:43 +00:00
*/
public function get_orders_count ( $status = null , $filter = array () ) {
2017-03-07 20:24:24 +00:00
if ( ! empty ( $status ) ) {
2014-07-12 21:49:43 +00:00
$filter [ 'status' ] = $status ;
2017-03-07 20:24:24 +00:00
}
2014-07-12 21:49:43 +00:00
$query = $this -> query_orders ( $filter );
2017-03-07 20:24:24 +00:00
if ( ! current_user_can ( 'read_private_shop_orders' ) ) {
2014-07-12 21:49:43 +00:00
return new WP_Error ( 'woocommerce_api_user_cannot_read_orders_count' , __ ( 'You do not have permission to read the orders count' , 'woocommerce' ), array ( 'status' => 401 ) );
2017-03-07 20:24:24 +00:00
}
2014-07-12 21:49:43 +00:00
return array ( 'count' => ( int ) $query -> found_posts );
}
/**
* Edit an order
*
* API v1 only allows updating the status of an order
*
* @ since 2.1
* @ param int $id the order ID
* @ param array $data
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2014-07-12 21:49:43 +00:00
*/
public function edit_order ( $id , $data ) {
$id = $this -> validate_request ( $id , 'shop_order' , 'edit' );
2017-03-07 20:24:24 +00:00
if ( is_wp_error ( $id ) ) {
2014-07-12 21:49:43 +00:00
return $id ;
2017-03-07 20:24:24 +00:00
}
2014-07-12 21:49:43 +00:00
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $id );
2014-07-12 21:49:43 +00:00
if ( ! empty ( $data [ 'status' ] ) ) {
$order -> update_status ( $data [ 'status' ], isset ( $data [ 'note' ] ) ? $data [ 'note' ] : '' );
}
return $this -> get_order ( $id );
}
/**
* Delete an order
*
* @ param int $id the order ID
* @ param bool $force true to permanently delete order , false to move to trash
* @ return array
*/
public function delete_order ( $id , $force = false ) {
$id = $this -> validate_request ( $id , 'shop_order' , 'delete' );
return $this -> delete ( $id , 'order' , ( 'true' === $force ) );
}
/**
* Get the admin order notes for an order
*
* @ since 2.1
* @ param int $id the order ID
* @ param string $fields fields to include in response
2017-05-15 11:50:52 +00:00
* @ return array | WP_Error
2014-07-12 21:49:43 +00:00
*/
public function get_order_notes ( $id , $fields = null ) {
// ensure ID is valid order ID
$id = $this -> validate_request ( $id , 'shop_order' , 'read' );
2017-03-07 20:24:24 +00:00
if ( is_wp_error ( $id ) ) {
2014-07-12 21:49:43 +00:00
return $id ;
2017-03-07 20:24:24 +00:00
}
2014-07-12 21:49:43 +00:00
$args = array (
'post_id' => $id ,
'approve' => 'approve' ,
2016-08-27 01:46:45 +00:00
'type' => 'order_note' ,
2014-07-12 21:49:43 +00:00
);
remove_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_order_comments' ), 10 , 1 );
$notes = get_comments ( $args );
add_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_order_comments' ), 10 , 1 );
$order_notes = array ();
foreach ( $notes as $note ) {
$order_notes [] = array (
'id' => $note -> comment_ID ,
'created_at' => $this -> server -> format_datetime ( $note -> comment_date_gmt ),
'note' => $note -> comment_content ,
'customer_note' => get_comment_meta ( $note -> comment_ID , 'is_customer_note' , true ) ? true : false ,
);
}
return array ( 'order_notes' => apply_filters ( 'woocommerce_api_order_notes_response' , $order_notes , $id , $fields , $notes , $this -> server ) );
}
/**
* Helper method to get order post objects
*
* @ since 2.1
* @ param array $args request arguments for filtering query
* @ return WP_Query
*/
private function query_orders ( $args ) {
// set base query arguments
$query_args = array (
'fields' => 'ids' ,
'post_type' => 'shop_order' ,
2016-08-27 01:46:45 +00:00
'post_status' => array_keys ( wc_get_order_statuses () ),
2014-07-12 21:49:43 +00:00
);
// add status argument
if ( ! empty ( $args [ 'status' ] ) ) {
2014-08-12 18:08:11 +00:00
$statuses = 'wc-' . str_replace ( ',' , ',wc-' , $args [ 'status' ] );
$statuses = explode ( ',' , $statuses );
2014-07-12 21:49:43 +00:00
$query_args [ 'post_status' ] = $statuses ;
unset ( $args [ 'status' ] );
}
$query_args = $this -> merge_query_args ( $query_args , $args );
return new WP_Query ( $query_args );
}
/**
* Helper method to get the order subtotal
*
* @ since 2.1
* @ param WC_Order $order
* @ return float
*/
private function get_order_subtotal ( $order ) {
$subtotal = 0 ;
// subtotal
foreach ( $order -> get_items () as $item ) {
2016-08-19 16:05:47 +00:00
$subtotal += $item -> get_subtotal ();
2014-07-12 21:49:43 +00:00
}
return $subtotal ;
}
}