Merge pull request #7280 from mattallan/matt/wc-api-orders-tweaks

Some more tweaks to WC API Orders
This commit is contained in:
Mike Jolley 2015-02-04 11:46:33 +00:00
commit 6af80f8e60
1 changed files with 33 additions and 17 deletions

View File

@ -19,6 +19,9 @@ class WC_API_Orders extends WC_API_Resource {
/** @var string $base the route base */
protected $base = '/orders';
/** @var string $post_type the custom post type */
protected $post_type = 'shop_order';
/**
* Register the routes for this class
*
@ -134,7 +137,7 @@ class WC_API_Orders extends WC_API_Resource {
public function get_order( $id, $fields = null, $filter = array() ) {
// ensure order ID is valid & user has permission to read
$id = $this->validate_request( $id, 'shop_order', 'read' );
$id = $this->validate_request( $id, $this->post_type, 'read' );
if ( is_wp_error( $id ) ) {
return $id;
@ -380,7 +383,7 @@ class WC_API_Orders extends WC_API_Resource {
}
// create the pending order
$order = wc_create_order( $default_order_args );
$order = $this->create_base_order( $default_order_args );
if ( is_wp_error( $order ) ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_create_order', sprintf( __( 'Cannot create order: %s', 'woocommerce' ), implode( ', ', $order->get_error_messages() ) ), 400 );
@ -465,6 +468,19 @@ class WC_API_Orders extends WC_API_Resource {
}
}
/**
* Creates new WC_Order.
*
* Requires a separate function for classes that extend WC_API_Orders.
*
* @since 2.3
* @param $args array
* @return WC_Order
*/
protected function create_base_order( $args ) {
return wc_create_order( $args );
}
/**
* Edit an order
*
@ -481,7 +497,7 @@ class WC_API_Orders extends WC_API_Resource {
$update_totals = false;
$id = $this->validate_request( $id, 'shop_order', 'edit' );
$id = $this->validate_request( $id, $this->post_type, 'edit' );
if ( is_wp_error( $id ) ) {
return $id;
@ -626,7 +642,7 @@ class WC_API_Orders extends WC_API_Resource {
*/
public function delete_order( $id, $force = false ) {
$id = $this->validate_request( $id, 'shop_order', 'delete' );
$id = $this->validate_request( $id, $this->post_type, 'delete' );
if ( is_wp_error( $id ) ) {
return $id;
@ -646,12 +662,12 @@ class WC_API_Orders extends WC_API_Resource {
* @param array $args request arguments for filtering query
* @return WP_Query
*/
private function query_orders( $args ) {
protected function query_orders( $args ) {
// set base query arguments
$query_args = array(
'fields' => 'ids',
'post_type' => 'shop_order',
'post_type' => $this->post_type,
'post_status' => array_keys( wc_get_order_statuses() )
);
@ -1108,7 +1124,7 @@ class WC_API_Orders extends WC_API_Resource {
public function get_order_notes( $order_id, $fields = null ) {
// ensure ID is valid order ID
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'read' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
@ -1148,7 +1164,7 @@ class WC_API_Orders extends WC_API_Resource {
public function get_order_note( $order_id, $id, $fields = null ) {
try {
// Validate order ID
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'read' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
@ -1196,10 +1212,10 @@ class WC_API_Orders extends WC_API_Resource {
throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_note', __( 'You do not have permission to create order notes', 'woocommerce' ), 401 );
}
$order_id = absint( $order_id );
$order_id = $this->validate_request( $order_id, $this->post_type, 'edit' );
if ( empty( $order_id ) ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_order_id', __( 'Order ID is invalid', 'woocommerce' ), 400 );
if ( is_wp_error( $order_id ) ) {
return $order_id;
}
$order = wc_get_order( $order_id );
@ -1245,7 +1261,7 @@ class WC_API_Orders extends WC_API_Resource {
$data = isset( $data['order_note'] ) ? $data['order_note'] : array();
// Validate order ID
$order_id = $this->validate_request( $order_id, 'shop_order', 'edit' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'edit' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
@ -1309,7 +1325,7 @@ class WC_API_Orders extends WC_API_Resource {
*/
public function delete_order_note( $order_id, $id ) {
try {
$order_id = $this->validate_request( $order_id, 'shop_order', 'delete' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'delete' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
@ -1360,7 +1376,7 @@ class WC_API_Orders extends WC_API_Resource {
public function get_order_refunds( $order_id, $fields = null ) {
// Ensure ID is valid order ID
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'read' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
@ -1395,7 +1411,7 @@ class WC_API_Orders extends WC_API_Resource {
public function get_order_refund( $order_id, $id, $fields = null ) {
try {
// Validate order ID
$order_id = $this->validate_request( $order_id, 'shop_order', 'read' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'read' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
@ -1548,7 +1564,7 @@ class WC_API_Orders extends WC_API_Resource {
$data = isset( $data['order_refund'] ) ? $data['order_refund'] : array();
// Validate order ID
$order_id = $this->validate_request( $order_id, 'shop_order', 'edit' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'edit' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
@ -1607,7 +1623,7 @@ class WC_API_Orders extends WC_API_Resource {
*/
public function delete_order_refund( $order_id, $id ) {
try {
$order_id = $this->validate_request( $order_id, 'shop_order', 'delete' );
$order_id = $this->validate_request( $order_id, $this->post_type, 'delete' );
if ( is_wp_error( $order_id ) ) {
return $order_id;