[REST API] Stops order creating if returns any error

Prevents orders of been created if a WC_Data_Exception or a WC_REST_Exception was throwed.
This commit is contained in:
Claudio Sanches 2019-04-17 00:36:20 -03:00
parent 9863362de3
commit 0f9ff3d70b
1 changed files with 19 additions and 0 deletions

View File

@ -144,6 +144,8 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
* @return WC_Data|WP_Error
*/
protected function save_object( $request, $creating = false ) {
$object = null;
try {
$object = $this->prepare_object_for_database( $request, $creating );
@ -196,12 +198,29 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller {
return $this->get_object( $object->get_id() );
} catch ( WC_Data_Exception $e ) {
$this->purge( $object, $creating );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
$this->purge( $object, $creating );
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}
/**
* Purge object when creating.
*
* @param WC_Data $object Object data.
* @param bool $creating If is creating a new object.
* @return bool
*/
protected function purge( $object, $creating ) {
if ( $object instanceof WC_Data && $creating ) {
return $object->delete( true );
}
return false;
}
/**
* Prepare objects query.
*