From 4d0e0b71f366bb21a3965b80dd7a5c2ef496474d Mon Sep 17 00:00:00 2001 From: matttallan Date: Fri, 13 Mar 2015 15:53:30 +1000 Subject: [PATCH] Rollback WCAPI create_order if exception is thrown after wc_create_order When you use the api to create an order and specify an method_id for the payment details, but no method_title. You recieve a WP_Error as the response but a pending order of $0 is still created. --- includes/api/class-wc-api-orders.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/includes/api/class-wc-api-orders.php b/includes/api/class-wc-api-orders.php index 2b4a94ee137..93b721b06e6 100644 --- a/includes/api/class-wc-api-orders.php +++ b/includes/api/class-wc-api-orders.php @@ -356,9 +356,12 @@ class WC_API_Orders extends WC_API_Resource { * @return array */ public function create_order( $data ) { + global $wpdb; $data = isset( $data['order'] ) ? $data['order'] : array(); + $wpdb->query( 'START TRANSACTION' ); + try { // permission check @@ -463,10 +466,14 @@ class WC_API_Orders extends WC_API_Resource { do_action( 'woocommerce_api_create_order', $order->id, $data, $this ); + $wpdb->query( 'COMMIT' ); + return $this->get_order( $order->id ); } catch ( WC_API_Exception $e ) { + $wpdb->query( 'ROLLBACK' ); + return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } }