[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:
parent
9863362de3
commit
0f9ff3d70b
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue