2013-11-04 06:36:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce API Orders Class
|
|
|
|
*
|
|
|
|
* Handles requests to the /orders endpoint
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
|
|
|
* @package WooCommerce/API
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2013-11-09 21:20:23 +00:00
|
|
|
class WC_API_Orders extends WC_API_Resource {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
/** @var string $base the route base */
|
|
|
|
protected $base = '/orders';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the routes for this class
|
|
|
|
*
|
2013-11-11 00:29:36 +00:00
|
|
|
* GET /orders
|
2013-11-04 06:36:31 +00:00
|
|
|
* GET /orders/count
|
|
|
|
* GET|PUT|DELETE /orders/<id>
|
|
|
|
* GET /orders/<id>/notes
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param array $routes
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function register_routes( $routes ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
# GET /orders
|
2013-11-04 06:36:31 +00:00
|
|
|
$routes[ $this->base ] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_orders' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# GET /orders/count
|
|
|
|
$routes[ $this->base . '/count'] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_orders_count' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# GET|PUT|DELETE /orders/<id>
|
|
|
|
$routes[ $this->base . '/(?P<id>\d+)' ] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_order' ), WC_API_Server::READABLE ),
|
|
|
|
array( array( $this, 'edit_order' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
|
|
|
|
array( array( $this, 'delete_order' ), WC_API_Server::DELETABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
# GET /orders/<id>/notes
|
|
|
|
$routes[ $this->base . '/(?P<id>\d+)/notes' ] = array(
|
2013-11-11 00:29:36 +00:00
|
|
|
array( array( $this, 'get_order_notes' ), WC_API_Server::READABLE ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $routes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all orders
|
|
|
|
*
|
|
|
|
* @since 2.1
|
2013-11-11 00:29:36 +00:00
|
|
|
* @param string $fields
|
|
|
|
* @param array $filter
|
2013-11-04 06:36:31 +00:00
|
|
|
* @param string $status
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_orders( $fields = null, $filter = array(), $status = null ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( ! empty( $status ) )
|
|
|
|
$filter['status'] = $status;
|
|
|
|
|
|
|
|
$query = $this->query_orders( $filter );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
$orders = array();
|
|
|
|
|
|
|
|
foreach( $query->posts as $order_id ) {
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$orders[] = $this->get_order( $order_id, $fields );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$this->server->query_navigation_headers( $query );
|
|
|
|
|
2013-11-04 06:36:31 +00:00
|
|
|
return array( 'orders' => $orders );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the order for the given ID
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param int $id the order ID
|
|
|
|
* @param array $fields
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_order( $id, $fields = null ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
// ensure order ID is valid & user has permission to read
|
|
|
|
$id = $this->validate_request( $id, 'shop_order', 'read' );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( is_wp_error( $id ) )
|
|
|
|
return $id;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
$order = new WC_Order( $id );
|
|
|
|
|
|
|
|
$order_data = array(
|
|
|
|
'id' => $order->id,
|
|
|
|
'order_number' => $order->get_order_number(),
|
|
|
|
'created_at' => $order->order_date,
|
|
|
|
'updated_at' => $order->modified_date,
|
|
|
|
'completed_at' => $order->completed_date,
|
|
|
|
'status' => $order->status,
|
|
|
|
'currency' => $order->order_currency,
|
2013-11-11 00:29:36 +00:00
|
|
|
'total' => (string) $order->get_total(),
|
|
|
|
'total_line_items_quantity' => (string) $order->get_item_count(),
|
|
|
|
'total_tax' => (string) $order->get_total_tax(),
|
|
|
|
'total_shipping' => (string) $order->get_total_shipping(),
|
|
|
|
'cart_tax' => (string) $order->get_cart_tax(),
|
|
|
|
'shipping_tax' => (string) $order->get_shipping_tax(),
|
|
|
|
'total_discount' => (string) $order->get_total_discount(),
|
|
|
|
'cart_discount' => (string) $order->get_cart_discount(),
|
|
|
|
'order_discount' => (string) $order->get_order_discount(),
|
2013-11-04 06:36:31 +00:00
|
|
|
'shipping_methods' => $order->get_shipping_method(),
|
|
|
|
'payment_details' => array(
|
|
|
|
'method_id' => $order->payment_method,
|
|
|
|
'method_title' => $order->payment_method_title,
|
|
|
|
'paid' => isset( $order->paid_date ),
|
|
|
|
),
|
|
|
|
'billing_address' => array(
|
|
|
|
'first_name' => $order->billing_first_name,
|
|
|
|
'last_name' => $order->billing_last_name,
|
|
|
|
'company' => $order->billing_company,
|
|
|
|
'address_1' => $order->billing_address_1,
|
|
|
|
'address_2' => $order->billing_address_2,
|
|
|
|
'city' => $order->billing_city,
|
|
|
|
'state' => $order->billing_state,
|
|
|
|
'postcode' => $order->billing_postcode,
|
|
|
|
'country' => $order->billing_country,
|
|
|
|
'email' => $order->billing_email,
|
|
|
|
'phone' => $order->billing_phone,
|
|
|
|
),
|
|
|
|
'shipping_address' => array(
|
|
|
|
'first_name' => $order->shipping_first_name,
|
|
|
|
'last_name' => $order->shipping_last_name,
|
|
|
|
'company' => $order->shipping_company,
|
|
|
|
'address_1' => $order->shipping_address_1,
|
|
|
|
'address_2' => $order->shipping_address_2,
|
|
|
|
'city' => $order->shipping_city,
|
|
|
|
'state' => $order->shipping_state,
|
|
|
|
'postcode' => $order->shipping_postcode,
|
|
|
|
'country' => $order->shipping_country,
|
|
|
|
),
|
|
|
|
'note' => $order->customer_note,
|
|
|
|
'customer_ip' => $order->customer_ip_address,
|
|
|
|
'customer_user_agent' => $order->customer_user_agent,
|
|
|
|
'customer_id' => $order->customer_user,
|
|
|
|
'view_order_url' => $order->get_view_order_url(),
|
2013-11-14 17:42:42 +00:00
|
|
|
'line_items' => array(),
|
|
|
|
'shipping_lines' => array(),
|
|
|
|
'tax_lines' => array(),
|
|
|
|
'fee_lines' => array(),
|
|
|
|
'coupon_lines' => array(),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// add line items
|
|
|
|
foreach( $order->get_items() as $item_id => $item ) {
|
|
|
|
|
|
|
|
$product = $order->get_product_from_item( $item );
|
|
|
|
|
|
|
|
$order_data['line_items'][] = array(
|
|
|
|
'id' => $item_id,
|
2013-11-11 00:29:36 +00:00
|
|
|
'subtotal' => (string) $order->get_line_subtotal( $item ),
|
|
|
|
'total' => (string) $order->get_line_total( $item ),
|
|
|
|
'total_tax' => (string) $order->get_line_tax( $item ),
|
|
|
|
'quantity' => (string) $item['qty'],
|
2013-11-04 06:36:31 +00:00
|
|
|
'tax_class' => ( ! empty( $item['tax_class'] ) ) ? $item['tax_class'] : null,
|
|
|
|
'name' => $item['name'],
|
|
|
|
'product_id' => ( isset( $product->variation_id ) ) ? $product->variation_id : $product->id,
|
|
|
|
'sku' => $product->get_sku(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add shipping
|
|
|
|
foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) {
|
|
|
|
|
|
|
|
$order_data['shipping_lines'][] = array(
|
|
|
|
'id' => $shipping_item_id,
|
|
|
|
'method_id' => $shipping_item['method_id'],
|
|
|
|
'method_title' => $shipping_item['name'],
|
2013-11-11 00:29:36 +00:00
|
|
|
'total' => (string) number_format( $shipping_item['cost'], 2 )
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add taxes
|
|
|
|
foreach ( $order->get_tax_totals() as $tax_code => $tax ) {
|
|
|
|
|
|
|
|
$order_data['tax_lines'][] = array(
|
|
|
|
'code' => $tax_code,
|
|
|
|
'title' => $tax->label,
|
2013-11-11 00:29:36 +00:00
|
|
|
'total' => (string) $tax->amount,
|
2013-11-04 06:36:31 +00:00
|
|
|
'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,
|
|
|
|
'title' => $fee_item['name'],
|
|
|
|
'tax_class' => ( ! empty( $fee_item['tax_class'] ) ) ? $fee_item['tax_class'] : null,
|
2013-11-11 00:29:36 +00:00
|
|
|
'total' => (string) $order->get_line_total( $fee_item ),
|
|
|
|
'total_tax' => (string) $order->get_line_tax( $fee_item ),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add coupons
|
|
|
|
foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) {
|
|
|
|
|
|
|
|
$order_data['coupon_lines'] = array(
|
|
|
|
'id' => $coupon_item_id,
|
|
|
|
'code' => $coupon_item['name'],
|
2013-11-11 00:29:36 +00:00
|
|
|
'amount' => (string) number_format( $coupon_item['discount_amount'], 2),
|
2013-11-04 06:36:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_api_order_response', $order_data, $order, $fields );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total number of orders
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param string $status
|
2013-11-11 00:29:36 +00:00
|
|
|
* @param array $filter
|
2013-11-04 06:36:31 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_orders_count( $status = null, $filter = array() ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( ! empty( $status ) )
|
|
|
|
$filter['status'] = $status;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$query = $this->query_orders( $filter );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
// TODO: permissions?
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
return array( 'count' => $query->found_posts );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
|
2013-11-04 06:36:31 +00:00
|
|
|
/**
|
|
|
|
* Edit an order
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param int $id the order ID
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function edit_order( $id, $data ) {
|
|
|
|
|
|
|
|
$id = $this->validate_request( $id, 'shop_order', 'write' );
|
|
|
|
|
|
|
|
if ( is_wp_error( $id ) )
|
|
|
|
return $id;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
// TODO: implement
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
return $this->get_order( $id );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an order
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param int $id the order ID
|
|
|
|
* @param bool $force true to permanently delete order, false to move to trash
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function delete_order( $id, $force = false ) {
|
|
|
|
|
|
|
|
$id = $this->validate_request( $id, 'shop_order', 'delete' );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
return $this->delete( $id, 'order', ( 'true' === $force ) );
|
2013-11-04 06:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the admin order notes for an order
|
|
|
|
* @param $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
public function get_order_notes( $id ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
// ensure ID is valid order ID
|
|
|
|
$id = $this->validate_request( $id, 'order', 'read' );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
if ( is_wp_error( $id ) )
|
|
|
|
return $id;
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
$args = array(
|
|
|
|
'post_id' => $id,
|
|
|
|
'approve' => 'approve',
|
|
|
|
'type' => 'order_note'
|
|
|
|
);
|
|
|
|
|
|
|
|
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(
|
2013-11-14 17:42:42 +00:00
|
|
|
'id' => $note->comment_ID,
|
2013-11-11 00:29:36 +00:00
|
|
|
'created_at' => $note->comment_date_gmt, // TODO: date formatting
|
2013-11-04 06:36:31 +00:00
|
|
|
'note' => $note->comment_content,
|
|
|
|
'customer_note' => get_comment_meta( $note->comment_ID, 'is_customer_note', true ) ? true : false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array( 'order_notes' => $order_notes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to get order post objects
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param array $args request arguments for filtering query
|
2013-11-11 00:29:36 +00:00
|
|
|
* @return WP_Query
|
2013-11-04 06:36:31 +00:00
|
|
|
*/
|
2013-11-11 00:29:36 +00:00
|
|
|
private function query_orders( $args ) {
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
// set base query arguments
|
|
|
|
$query_args = array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'post_type' => 'shop_order',
|
|
|
|
'post_status' => 'publish',
|
|
|
|
);
|
|
|
|
|
|
|
|
// add status argument
|
|
|
|
if ( ! empty( $args['status'] ) ) {
|
|
|
|
|
|
|
|
$statuses = explode( ',', $args['status'] );
|
|
|
|
|
|
|
|
$query_args['tax_query'] = array(
|
|
|
|
array(
|
|
|
|
'taxonomy' => 'shop_order_status',
|
2013-11-11 00:29:36 +00:00
|
|
|
'field' => 'slug',
|
|
|
|
'terms' => $statuses,
|
2013-11-04 06:36:31 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
unset( $args['status'] );
|
|
|
|
}
|
2013-11-04 06:36:31 +00:00
|
|
|
|
2013-11-11 00:29:36 +00:00
|
|
|
$query_args = $this->merge_query_args( $query_args, $args );
|
2013-11-04 06:36:31 +00:00
|
|
|
|
|
|
|
return new WP_Query( $query_args );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|