2016-02-17 19:29:09 +00:00
< ? php
/**
* REST API Orders controller
*
* Handles requests to the / orders endpoint .
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / API
* @ since 2.6 . 0
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* REST API Orders controller class .
*
* @ package WooCommerce / API
2016-02-22 19:43:52 +00:00
* @ extends WC_REST_Posts_Controller
2016-02-17 19:29:09 +00:00
*/
2016-02-22 19:43:52 +00:00
class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
2016-02-17 19:29:09 +00:00
2016-03-07 18:36:17 +00:00
/**
* Endpoint namespace .
*
* @ var string
*/
2016-03-07 18:45:10 +00:00
public $namespace = 'wc/v1' ;
2016-03-07 18:36:17 +00:00
2016-02-17 19:29:09 +00:00
/**
* Route base .
*
* @ var string
*/
2016-02-22 18:49:38 +00:00
protected $rest_base = 'orders' ;
2016-02-17 19:29:09 +00:00
/**
2016-02-22 19:43:52 +00:00
* Post type .
2016-02-17 19:29:09 +00:00
*
* @ var string
*/
2016-02-22 19:43:52 +00:00
protected $post_type = 'shop_order' ;
2016-02-17 19:29:09 +00:00
2016-03-23 22:19:08 +00:00
/**
* Initialize orders actions .
*/
public function __construct () {
add_filter ( " woocommerce_rest_ { $this -> post_type } _query " , array ( $this , 'query_args' ), 10 , 2 );
}
2016-02-17 19:29:09 +00:00
/**
2016-02-23 18:34:42 +00:00
* Register the routes for orders .
2016-02-17 19:29:09 +00:00
*/
public function register_routes () {
2016-03-23 11:30:58 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base , array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
),
array (
'methods' => WP_REST_Server :: CREATABLE ,
'callback' => array ( $this , 'create_item' ),
'permission_callback' => array ( $this , 'create_item_permissions_check' ),
2016-03-24 20:25:10 +00:00
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: CREATABLE ),
2016-03-23 11:30:58 +00:00
),
'schema' => array ( $this , 'get_public_item_schema' ),
) );
2016-03-07 18:36:17 +00:00
register_rest_route ( $this -> namespace , '/' . $this -> rest_base . '/(?P<id>[\d]+)' , array (
2016-03-23 11:30:58 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_item' ),
'permission_callback' => array ( $this , 'get_item_permissions_check' ),
'args' => array (
'context' => $this -> get_context_param ( array ( 'default' => 'view' ) ),
),
),
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'update_item' ),
'permission_callback' => array ( $this , 'update_item_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: EDITABLE ),
),
2016-02-23 18:34:42 +00:00
array (
'methods' => WP_REST_Server :: DELETABLE ,
'callback' => array ( $this , 'delete_item' ),
'permission_callback' => array ( $this , 'delete_item_permissions_check' ),
'args' => array (
'force' => array (
'default' => false ,
2016-03-23 11:30:58 +00:00
'description' => __ ( 'Required to be true, as resource does not support trashing.' , 'woocommerce' ),
2016-02-23 18:34:42 +00:00
),
2016-03-23 11:30:58 +00:00
'reassign' => array (),
2016-02-23 18:34:42 +00:00
),
),
2016-03-23 11:30:58 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
2016-02-23 18:34:42 +00:00
) );
2016-02-17 19:29:09 +00:00
}
2016-03-23 11:30:58 +00:00
/**
* Check whether a given request has permission to read orders .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_items_permissions_check ( $request ) {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot list orders.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Check if a given request has access create orders .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return boolean
*/
public function create_item_permissions_check ( $request ) {
2016-03-24 20:25:10 +00:00
if ( ! current_user_can ( 'publish_shop_orders' ) ) {
2016-03-23 11:30:58 +00:00
return new WP_Error ( 'woocommerce_rest_cannot_create' , __ ( 'Sorry, you are not allowed to create resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Check if a given request has access to read an order .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_item_permissions_check ( $request ) {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot view this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Check if a given request has access update an order .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return boolean
*/
public function update_item_permissions_check ( $request ) {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_edit' , __ ( 'Sorry, you are not allowed to edit resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Check if a given request has access delete an order .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return boolean
*/
public function delete_item_permissions_check ( $request ) {
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_delete' , __ ( 'Sorry, you are not allowed to delete this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
2016-03-23 22:19:08 +00:00
* Prepare a single coupon output for response .
*
* @ param WP_Post $post Post object .
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $data
*/
public function prepare_item_for_response ( $post , $request ) {
global $wpdb ;
$order = wc_get_order ( $post );
$dp = ! empty ( $request [ 'dp' ] ) ? intval ( $request [ 'dp' ] ) : 2 ;
$data = array (
'id' => $order -> id ,
'parent_id' => $post -> post_parent ,
'status' => $order -> get_status (),
'order_key' => $order -> order_key ,
'currency' => $order -> get_order_currency (),
'version' => $order -> order_version ,
'prices_include_tax' => $order -> prices_include_tax ,
'date_created' => wc_rest_api_prepare_date_response ( $post -> post_date_gmt ),
'date_modified' => wc_rest_api_prepare_date_response ( $post -> post_modified_gmt ),
'customer_id' => $order -> get_user_id (),
'discount_total' => wc_format_decimal ( $order -> get_total_discount (), $dp ),
'discount_tax' => wc_format_decimal ( $order -> cart_discount_tax , $dp ),
'shipping_total' => wc_format_decimal ( $order -> get_total_shipping (), $dp ),
'shipping_tax' => wc_format_decimal ( $order -> get_shipping_tax (), $dp ),
'cart_tax' => wc_format_decimal ( $order -> get_cart_tax (), $dp ),
'total' => wc_format_decimal ( $order -> get_total (), $dp ),
'total_tax' => wc_format_decimal ( $order -> get_total_tax (), $dp ),
'billing' => array (),
'shipping' => array (),
'payment_method' => $order -> payment_method ,
'payment_method_title' => $order -> payment_method_title ,
'transaction_id' => $order -> get_transaction_id (),
'customer_ip_address' => $order -> customer_ip_address ,
'customer_user_agent' => $order -> customer_user_agent ,
'created_via' => $order -> created_via ,
'customer_note' => $order -> customer_note ,
'date_completed' => wc_rest_api_prepare_date_response ( $order -> completed_date , true ),
'date_paid' => $order -> paid_date ,
'cart_hash' => $order -> cart_hash ,
'line_items' => array (),
'tax_lines' => array (),
'shipping_lines' => array (),
'fee_lines' => array (),
'coupon_lines' => array (),
);
// Add addresses.
$data [ 'billing' ] = $order -> get_address ( 'billing' );
$data [ 'shipping' ] = $order -> get_address ( 'shipping' );
// Add line items.
foreach ( $order -> get_items () as $item_id => $item ) {
$product = $order -> get_product_from_item ( $item );
$product_id = 0 ;
$variation_id = 0 ;
$product_sku = null ;
// Check if the product exists.
if ( is_object ( $product ) ) {
$product_id = $product -> id ;
$variation_id = $product -> variation_id ;
$product_sku = $product -> get_sku ();
}
$meta = new WC_Order_Item_Meta ( $item , $product );
$item_meta = array ();
$hideprefix = 'true' === $request [ 'all_item_meta' ] ? null : '_' ;
foreach ( $meta -> get_formatted ( $hideprefix ) as $meta_key => $formatted_meta ) {
$item_meta [] = array (
'key' => $formatted_meta [ 'key' ],
'label' => $formatted_meta [ 'label' ],
'value' => $formatted_meta [ 'value' ],
);
}
$line_item = array (
'id' => $item_id ,
'name' => $item [ 'name' ],
'sku' => $product_sku ,
'product_id' => ( int ) $product_id ,
'variation_id' => ( int ) $variation_id ,
'quantity' => wc_stock_amount ( $item [ 'qty' ] ),
'tax_class' => ! empty ( $item [ 'tax_class' ] ) ? $item [ 'tax_class' ] : '' ,
'price' => wc_format_decimal ( $order -> get_item_total ( $item , false , false ), $dp ),
'subtotal' => wc_format_decimal ( $order -> get_line_subtotal ( $item , false , false ), $dp ),
'subtotal_tax' => wc_format_decimal ( $item [ 'line_subtotal_tax' ], $dp ),
'total' => wc_format_decimal ( $order -> get_line_total ( $item , false , false ), $dp ),
'total_tax' => wc_format_decimal ( $item [ 'line_tax' ], $dp ),
'taxes' => array (),
'meta' => $item_meta ,
);
$item_line_taxes = maybe_unserialize ( $item [ 'line_tax_data' ] );
if ( isset ( $item_line_taxes [ 'total' ] ) ) {
$line_tax = array ();
foreach ( $item_line_taxes [ 'total' ] as $tax_rate_id => $tax ) {
$line_tax [ $tax_rate_id ] = array (
'id' => $tax_rate_id ,
'total' => $tax ,
'subtotal' => '' ,
);
}
foreach ( $item_line_taxes [ 'subtotal' ] as $tax_rate_id => $tax ) {
$line_tax [ $tax_rate_id ][ 'subtotal' ] = $tax ;
}
$line_item [ 'taxes' ] = array_values ( $line_tax );
}
// if ( in_array( 'products', $expand ) ) {
// $_product_data = WC()->api->WC_API_Products->get_product( $product_id );
// if ( isset( $_product_data['product'] ) ) {
// $line_item['product_data'] = $_product_data['product'];
// }
// }
$data [ 'line_items' ][] = $line_item ;
}
// Add taxes.
foreach ( $order -> get_items ( 'tax' ) as $key => $tax ) {
$tax_line = array (
'id' => $key ,
'rate_code' => $tax [ 'name' ],
'rate_id' => $tax [ 'rate_id' ],
'label' => isset ( $tax [ 'label' ] ) ? $tax [ 'label' ] : $tax [ 'name' ],
'compound' => ( bool ) $tax [ 'compound' ],
'tax_total' => wc_format_decimal ( $tax [ 'tax_amount' ], $dp ),
'shipping_tax_total' => wc_format_decimal ( $tax [ 'shipping_tax_amount' ], $dp ),
);
// if ( in_array( 'taxes', $expand ) ) {
// $_rate_data = WC()->api->WC_API_Taxes->get_tax( $tax->rate_id );
// if ( isset( $_rate_data['tax'] ) ) {
// $tax_line['rate_data'] = $_rate_data['tax'];
// }
// }
$data [ 'tax_lines' ][] = $tax_line ;
}
// Add shipping.
foreach ( $order -> get_shipping_methods () as $shipping_item_id => $shipping_item ) {
$shipping_line = array (
'id' => $shipping_item_id ,
'method_title' => $shipping_item [ 'name' ],
'method_id' => $shipping_item [ 'method_id' ],
'total' => wc_format_decimal ( $shipping_item [ 'cost' ], $dp ),
'total_tax' => wc_format_decimal ( '' , $dp ),
'taxes' => array (),
);
$shipping_taxes = maybe_unserialize ( $shipping_item [ 'taxes' ] );
if ( ! empty ( $shipping_taxes ) ) {
$shipping_line [ 'total_tax' ] = wc_format_decimal ( array_sum ( $shipping_taxes ), $dp );
foreach ( $shipping_taxes as $tax_rate_id => $tax ) {
$shipping_line [ 'taxes' ][] = array (
'id' => $tax_rate_id ,
'total' => $tax ,
);
}
}
$data [ 'shipping_lines' ][] = $shipping_line ;
}
// Add fees.
foreach ( $order -> get_fees () as $fee_item_id => $fee_item ) {
$fee_line = array (
'id' => $fee_item_id ,
'name' => $fee_item [ 'name' ],
'tax_class' => ! empty ( $fee_item [ 'tax_class' ] ) ? $fee_item [ 'tax_class' ] : '' ,
'tax_status' => 'taxable' ,
'total' => wc_format_decimal ( $order -> get_line_total ( $fee_item ), $dp ),
'total_tax' => wc_format_decimal ( $order -> get_line_tax ( $fee_item ), $dp ),
'taxes' => array (),
);
$fee_line_taxes = maybe_unserialize ( $fee_item [ 'line_tax_data' ] );
if ( isset ( $fee_line_taxes [ 'total' ] ) ) {
$fee_tax = array ();
foreach ( $fee_line_taxes [ 'total' ] as $tax_rate_id => $tax ) {
$fee_tax [ $tax_rate_id ] = array (
'id' => $tax_rate_id ,
'total' => $tax ,
'subtotal' => '' ,
);
}
foreach ( $fee_line_taxes [ 'subtotal' ] as $tax_rate_id => $tax ) {
$fee_tax [ $tax_rate_id ][ 'subtotal' ] = $tax ;
}
$fee_line [ 'taxes' ] = array_values ( $fee_tax );
}
$data [ 'fee_lines' ][] = $fee_line ;
}
// Add coupons.
foreach ( $order -> get_items ( 'coupon' ) as $coupon_item_id => $coupon_item ) {
$coupon_line = array (
'id' => $coupon_item_id ,
'code' => $coupon_item [ 'name' ],
'discount' => wc_format_decimal ( $coupon_item [ 'discount_amount' ], $dp ),
'discount_tax' => wc_format_decimal ( $coupon_item [ 'discount_amount_tax' ], $dp ),
);
// if ( in_array( 'coupons', $expand ) ) {
// $_coupon_data = WC()->api->WC_API_Coupons->get_coupon_by_code( $coupon_item['name'] );
// if ( isset( $_coupon_data['coupon'] ) ) {
// $coupon_line['coupon_data'] = $_coupon_data['coupon'];
// }
// }
$data [ 'coupon_lines' ][] = $coupon_line ;
}
$context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ;
$data = $this -> add_additional_fields_to_object ( $data , $request );
$data = $this -> filter_response_by_context ( $data , $context );
// Wrap the data in a response object.
$response = rest_ensure_response ( $data );
$response -> add_links ( $this -> prepare_links ( $order ) );
/**
* Filter the data for a response .
*
* The dynamic portion of the hook name , $this -> post_type , refers to post_type of the post being
* prepared for the response .
*
* @ param WP_REST_Response $response The response object .
* @ param WP_Post $post Post object .
* @ param WP_REST_Request $request Request object .
*/
return apply_filters ( " woocommerce_rest_prepare_ { $this -> post_type } " , $response , $post , $request );
}
/**
* Prepare links for the request .
*
* @ param WC_Order $order Comment object .
* @ return array Links for the given order .
*/
protected function prepare_links ( $order ) {
$links = array (
'self' => array (
'href' => rest_url ( sprintf ( '/%s/%s/%d' , $this -> namespace , $this -> rest_base , $order -> id ) ),
),
'collection' => array (
'href' => rest_url ( sprintf ( '/%s/%s' , $this -> namespace , $this -> rest_base ) ),
),
);
if ( 0 !== ( int ) $order -> get_user_id () ) {
$links [ 'customer' ] = array (
'href' => rest_url ( sprintf ( '/%s/customers/%d' , $this -> namespace , $order -> get_user_id () ) ),
);
}
if ( 0 !== ( int ) $order -> post -> post_parent ) {
$links [ 'up' ] = array (
'href' => rest_url ( sprintf ( '/%s/orders/%d' , $this -> namespace , $order -> post -> post_parent ) ),
);
}
return $links ;
}
/**
* Query args .
2016-03-23 11:30:58 +00:00
*
2016-03-23 22:19:08 +00:00
* @ param array $args
* @ param WP_REST_Request $request
2016-03-23 11:30:58 +00:00
* @ return array
*/
2016-03-23 22:19:08 +00:00
public function query_args ( $args , $request ) {
// Set post_status.
if ( 'any' !== $request [ 'status' ] ) {
$args [ 'post_status' ] = 'wc-' . $request [ 'status' ];
} else {
$args [ 'post_status' ] = 'any' ;
}
return $args ;
}
2016-03-24 20:25:10 +00:00
/**
* Create order .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return int | WP_Error
*/
protected function create_order ( $request ) {
wc_transaction_query ( 'start' );
try {
$order = wc_create_order ( array (
'status' => $request [ 'status' ],
'customer_id' => $request [ 'customer_id' ],
'customer_note' => $request [ 'customer_note' ],
'created_via' => 'rest-api' ,
) );
if ( is_wp_error ( $order ) ) {
throw new Exception ( sprintf ( __ ( 'Cannot create order: %s.' , 'woocommerce' ), implode ( ', ' , $order -> get_error_messages () ) ), 400 );
}
// Set addresses.
if ( ! empty ( $request [ 'billing' ] ) ) {
$this -> update_address ( $order , $request [ 'billing' ], 'billing' );
}
if ( ! empty ( $request [ 'shipping' ] ) ) {
$this -> update_address ( $order , $request [ 'shipping' ], 'shipping' );
}
2016-03-24 20:43:14 +00:00
// Set currency.
update_post_meta ( $order -> id , '_order_currency' , $request [ 'currency' ] );
// Set payment method.
if ( ! empty ( $request [ 'payment_method' ] ) ) {
update_post_meta ( $order -> id , '_payment_method' , $request [ 'payment_method' ] );
}
if ( ! empty ( $request [ 'payment_method_title' ] ) ) {
update_post_meta ( $order -> id , '_payment_method_title' , $request [ 'payment_method' ] );
}
if ( true === $request [ 'set_paid' ] ) {
$order -> payment_complete ( $request [ 'transaction_id' ] );
}
// TODO
// Set order meta.
// if ( isset( $data['order_meta'] ) && is_array( $data['order_meta'] ) ) {
// $this->set_order_meta( $order->id, $data['order_meta'] );
// }
2016-03-24 20:25:10 +00:00
wc_transaction_query ( 'commit' );
return $order -> id ;
} catch ( Exception $e ) {
wc_transaction_query ( 'rollback' );
return new WP_Error ( " woocommerce_rest_ { $this -> post_type } _create_error " , $e -> getMessage (), array ( 'status' => $e -> getCode () ) );
}
}
/**
* Update address .
*
* @ param WC_Order $order
* @ param array $posted
* @ param string $type
*/
protected function update_address ( $order , $posted , $type = 'billing' ) {
$fields = $order -> get_address ( $type );
foreach ( array_keys ( $fields ) as $field ) {
if ( isset ( $posted [ $field ] ) ) {
$fields [ $field ] = $posted [ $field ];
}
}
// Set address.
$order -> set_address ( $fields , $type );
// Update user meta.
if ( $order -> get_user_id () ) {
foreach ( $fields as $key => $value ) {
update_user_meta ( $order -> get_user_id (), $type . '_' . $key , $value );
}
}
}
/**
* Create a single item .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | WP_REST_Response
*/
public function create_item ( $request ) {
if ( ! empty ( $request [ 'id' ] ) ) {
return new WP_Error ( " woocommerce_rest_ { $this -> post_type } _exists " , sprintf ( __ ( 'Cannot create existing %s.' , 'woocommerce' ), $this -> post_type ), array ( 'status' => 400 ) );
}
$order_id = $this -> create_order ( $request );
if ( is_wp_error ( $order_id ) ) {
return $order_id ;
}
$post = get_post ( $order_id );
$this -> update_additional_fields_for_object ( $post , $request );
/**
* Fires after a single item is created or updated via the REST API .
*
* @ param object $post Inserted object ( not a WP_Post object ) .
* @ param WP_REST_Request $request Request object .
* @ param boolean $creating True when creating item , false when updating .
*/
do_action ( " woocommerce_rest_insert_ { $this -> post_type } " , $post , $request , true );
$request -> set_param ( 'context' , 'edit' );
$response = $this -> prepare_item_for_response ( $post , $request );
$response = rest_ensure_response ( $response );
$response -> set_status ( 201 );
$response -> header ( 'Location' , rest_url ( sprintf ( '/%s/%s/%d' , $this -> namespace , $this -> rest_base , $post -> ID ) ) );
return $response ;
}
2016-03-23 22:19:08 +00:00
/**
* Get order statuses .
*
* @ return array
*/
protected function get_order_statuses () {
2016-03-23 11:30:58 +00:00
$order_statuses = array ();
foreach ( array_keys ( wc_get_order_statuses () ) as $status ) {
$order_statuses [] = str_replace ( 'wc-' , '' , $status );
}
2016-03-23 22:19:08 +00:00
return $order_statuses ;
}
/**
* Get the Order ' s schema , conforming to JSON Schema .
*
* @ return array
*/
public function get_item_schema () {
2016-03-23 11:30:58 +00:00
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
'title' => $this -> post_type ,
'type' => 'object' ,
'properties' => array (
'id' => array (
'description' => __ ( 'Unique identifier for the resource.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'parent_id' => array (
'description' => __ ( 'Parent order ID.' , 'woocommerce' ),
2016-03-23 11:30:58 +00:00
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
2016-03-23 12:46:34 +00:00
),
'status' => array (
'description' => __ ( 'Order status.' , 'woocommerce' ),
'type' => 'string' ,
'default' => 'pending' ,
2016-03-23 22:19:08 +00:00
'enum' => $this -> get_order_statuses (),
2016-03-23 12:46:34 +00:00
'context' => array ( 'view' , 'edit' ),
2016-03-23 11:30:58 +00:00
),
'order_key' => array (
'description' => __ ( 'Order key.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'currency' => array (
'description' => __ ( 'Currency the order was created with, in ISO format.' , 'woocommerce' ),
'type' => 'string' ,
2016-03-24 20:43:14 +00:00
'default' => get_woocommerce_currency (),
2016-03-23 12:46:34 +00:00
'enum' => array_keys ( get_woocommerce_currencies () ),
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
),
2016-03-23 12:46:34 +00:00
'version' => array (
'description' => __ ( 'Version of WooCommerce when the order was made.' , 'woocommerce' ),
'type' => 'integer' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'prices_include_tax' => array (
'description' => __ ( 'Shows if the prices included tax during checkout.' , 'woocommerce' ),
'type' => 'boolean' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'date_created' => array (
'description' => __ ( " The date the order was created, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'date_modified' => array (
'description' => __ ( " The date the order was last modified, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'customer_id' => array (
'description' => __ ( 'User ID who owns the order. 0 for guests.' , 'woocommerce' ),
2016-03-23 11:30:58 +00:00
'type' => 'integer' ,
2016-03-23 12:46:34 +00:00
'default' => 0 ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
),
2016-03-23 12:46:34 +00:00
'discount_total' => array (
'description' => __ ( 'Total discount amount for the order.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'string' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'discount_tax' => array (
'description' => __ ( 'Total discount tax amount for the order.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'string' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'shipping_total' => array (
'description' => __ ( 'Total shipping amount for the order.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'string' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'shipping_tax' => array (
2016-03-23 12:46:34 +00:00
'description' => __ ( 'Total shipping tax amount for the order.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'string' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'cart_tax' => array (
'description' => __ ( 'Sum of line item taxes only.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'string' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'total' => array (
'description' => __ ( 'Grand total.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'string' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'total_tax' => array (
'description' => __ ( 'Sum of all taxes.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'string' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'billing' => array (
2016-03-23 11:30:58 +00:00
'description' => __ ( 'Billing address.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'array' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'first_name' => array (
'description' => __ ( 'First name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'last_name' => array (
'description' => __ ( 'Last name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'company' => array (
'description' => __ ( 'Company name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'address_1' => array (
'description' => __ ( 'Address line 1.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'address_2' => array (
'description' => __ ( 'Address line 2.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'city' => array (
'description' => __ ( 'City name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'state' => array (
'description' => __ ( 'ISO code or name of the state, province or district.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'postcode' => array (
'description' => __ ( 'Postal code.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'country' => array (
2016-03-23 12:46:34 +00:00
'description' => __ ( 'Country code in ISO 3166-1 alpha-2 format.' , 'woocommerce' ),
2016-03-23 11:30:58 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'email' => array (
'description' => __ ( 'Email address.' , 'woocommerce' ),
'type' => 'string' ,
'format' => 'email' ,
'context' => array ( 'view' , 'edit' ),
),
'phone' => array (
'description' => __ ( 'Phone number.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
2016-03-23 12:46:34 +00:00
'shipping' => array (
2016-03-23 11:30:58 +00:00
'description' => __ ( 'Shipping address.' , 'woocommerce' ),
2016-03-23 22:19:08 +00:00
'type' => 'array' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
'properties' => array (
'first_name' => array (
'description' => __ ( 'First name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'last_name' => array (
'description' => __ ( 'Last name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'company' => array (
'description' => __ ( 'Company name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'address_1' => array (
'description' => __ ( 'Address line 1.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'address_2' => array (
'description' => __ ( 'Address line 2.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'city' => array (
'description' => __ ( 'City name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'state' => array (
'description' => __ ( 'ISO code or name of the state, province or district.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'postcode' => array (
'description' => __ ( 'Postal code.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'country' => array (
2016-03-23 12:46:34 +00:00
'description' => __ ( 'Country code in ISO 3166-1 alpha-2 format.' , 'woocommerce' ),
2016-03-23 11:30:58 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
),
),
2016-03-23 12:46:34 +00:00
'payment_method' => array (
'description' => __ ( 'Payment method ID.' , 'woocommerce' ),
2016-03-23 11:30:58 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-03-23 12:46:34 +00:00
'payment_method_title' => array (
'description' => __ ( 'Payment method title.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2016-03-24 20:43:14 +00:00
'set_paid' => array (
'description' => __ ( 'Define if the order is paid. It will set the status to processing and reduce stock items.' , 'woocommerce' ),
'type' => 'boolean' ,
'default' => false ,
'context' => array ( 'view' , 'edit' ),
'writeonly' => true ,
),
2016-03-23 12:46:34 +00:00
'transaction_id' => array (
'description' => __ ( 'Unique transaction ID.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
),
'customer_ip_address' => array (
'description' => __ ( " Customer's IP address. " , 'woocommerce' ),
2016-03-23 11:30:58 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'customer_user_agent' => array (
2016-03-23 12:46:34 +00:00
'description' => __ ( 'User agent of the customer.' , 'woocommerce' ),
2016-03-23 11:30:58 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 12:46:34 +00:00
'created_via' => array (
'description' => __ ( 'Shows where the order was created.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'customer_note' => array (
'description' => __ ( 'Note left by customer during checkout.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'date_completed' => array (
'description' => __ ( " The date the order was completed, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'date_paid' => array (
'description' => __ ( " The date the order has been paid, in the site's timezone. " , 'woocommerce' ),
'type' => 'date-time' ,
2016-03-23 11:30:58 +00:00
'context' => array ( 'view' , 'edit' ),
2016-03-23 12:46:34 +00:00
'readonly' => true ,
),
'cart_hash' => array (
'description' => __ ( 'MD5 hash of cart items to ensure orders are not modified.' , 'woocommerce' ),
'type' => 'float' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
2016-03-23 11:30:58 +00:00
),
'line_items' => array (
'description' => __ ( 'Last order data.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2016-03-23 22:19:08 +00:00
'id' => array (
'description' => __ ( 'Item ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'name' => array (
'description' => __ ( 'Product name.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'sku' => array (
'description' => __ ( 'Product SKU.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'product_id' => array (
'description' => __ ( 'Product ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'variation_id' => array (
'description' => __ ( 'Variation ID, if applicable.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'quantity' => array (
'description' => __ ( 'Quantity ordered.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'tax_class' => array (
'description' => __ ( 'Tax class of product.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'price' => array (
'description' => __ ( 'Product price.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'subtotal' => array (
'description' => __ ( 'Line subtotal (before discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'subtotal_tax' => array (
'description' => __ ( 'Line subtotal tax (before discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total' => array (
'description' => __ ( 'Line total (after discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total_tax' => array (
'description' => __ ( 'Line total tax (after discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'taxes' => array (
'description' => __ ( 'Line total tax.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
'properties' => array (
'id' => array (
'description' => __ ( 'Tax rate ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total' => array (
'description' => __ ( 'Tax total.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'subtotal' => array (
'description' => __ ( 'Tax subtotal.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
),
),
'meta' => array (
'description' => __ ( 'Line item meta data.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
'properties' => array (
'key' => array (
'description' => __ ( 'Meta key.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'label' => array (
'description' => __ ( 'Meta label.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'value' => array (
'description' => __ ( 'Meta value.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
),
),
2016-03-23 11:30:58 +00:00
),
),
2016-03-23 22:19:08 +00:00
'tax_lines' => array (
2016-03-23 11:30:58 +00:00
'description' => __ ( 'Last order data.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
2016-03-23 22:19:08 +00:00
'readonly' => true ,
2016-03-23 11:30:58 +00:00
'properties' => array (
2016-03-23 22:19:08 +00:00
'id' => array (
'description' => __ ( 'Item ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'rate_code' => array (
'description' => __ ( 'Tax rate code.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'rate_id' => array (
'description' => __ ( 'Tax rate ID.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'label' => array (
'description' => __ ( 'Tax rate label.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'compound' => array (
'description' => __ ( 'Show if is a compound tax rate.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'tax_total' => array (
'description' => __ ( 'Tax total (not including shipping taxes).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'shipping_tax_total' => array (
'description' => __ ( 'Shipping tax total.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 11:30:58 +00:00
),
),
2016-03-23 22:19:08 +00:00
'shipping_lines' => array (
2016-03-23 11:30:58 +00:00
'description' => __ ( 'Last order data.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2016-03-23 22:19:08 +00:00
'id' => array (
'description' => __ ( 'Item ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'method_title' => array (
'description' => __ ( 'Shipping method name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'method_id' => array (
'description' => __ ( 'Shipping method ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
),
'total' => array (
'description' => __ ( 'Line total (after discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total_tax' => array (
'description' => __ ( 'Line total tax (after discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'taxes' => array (
'description' => __ ( 'Line total tax.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
'properties' => array (
'id' => array (
'description' => __ ( 'Tax rate ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total' => array (
'description' => __ ( 'Tax total.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
),
),
2016-03-23 11:30:58 +00:00
),
),
'fee_lines' => array (
'description' => __ ( 'Last order data.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2016-03-23 22:19:08 +00:00
'id' => array (
'description' => __ ( 'Item ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'name' => array (
'description' => __ ( 'Fee name.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'tax_class' => array (
'description' => __ ( 'Tax class of fee.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'tax_status' => array (
'description' => __ ( 'Tax status of fee.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total' => array (
'description' => __ ( 'Line total tax (after discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total_tax' => array (
'description' => __ ( 'Line total tax (after discounts).' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'taxes' => array (
'description' => __ ( 'Line total tax.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
'properties' => array (
'id' => array (
'description' => __ ( 'Tax rate ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'total' => array (
'description' => __ ( 'Tax total.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'subtotal' => array (
'description' => __ ( 'Tax subtotal.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
),
),
2016-03-23 11:30:58 +00:00
),
),
'coupon_lines' => array (
'description' => __ ( 'Last order data.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
'properties' => array (
2016-03-23 22:19:08 +00:00
'id' => array (
'description' => __ ( 'Item ID.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'code' => array (
'description' => __ ( 'Coupon code.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'discount' => array (
'description' => __ ( 'Discount total.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
'discount_tax' => array (
'description' => __ ( 'Discount total tax.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2016-03-23 11:30:58 +00:00
),
),
),
);
return $this -> add_additional_fields_schema ( $schema );
}
2016-03-23 22:19:08 +00:00
/**
* Get the query params for collections of attachments .
*
* @ return array
*/
public function get_collection_params () {
$params = parent :: get_collection_params ();
$params [ 'status' ] = array (
'default' => 'any' ,
'description' => __ ( 'Limit result set to orders assigned a specific status.' , 'woocommerce' ),
'type' => 'string' ,
'enum' => array_merge ( array ( 'any' ), $this -> get_order_statuses () ),
'sanitize_callback' => 'sanitize_key' ,
'validate_callback' => 'rest_validate_request_arg' ,
);
return $params ;
}
2016-02-17 19:29:09 +00:00
}