WC_REST_Order_Refunds_Controller::prepare_object_for_database
Prepares one object for create or update operation.
Method of the class: WC_REST_Order_Refunds_Controller{}
Hooks from the method
Returns
WP_Error|WC_Data. The prepared item, or WP_Error object on failure.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_object_for_database( $request, $creating );
- $request(WP_REST_Request) (required)
- Request object.
- $creating(true|false)
- If is creating a new object.
Default:false
Changelog
| Since 3.0.0 | Introduced. |
WC_REST_Order_Refunds_Controller::prepare_object_for_database() WC REST Order Refunds Controller::prepare object for database code WC 11.3.0
protected function prepare_object_for_database( $request, $creating = false ) {
// The opt-in compute_totals mode routes through the shared wc/v4 refund
// calculation pipeline. It is a separate path so that requests without the
// flag behave exactly as before, including degenerate forms such as
// quantity-only line items producing a 0.00 refund. The schema declares
// compute_totals as boolean with a false default, so the REST layer has
// already sanitized the value by the time this runs.
if ( $creating && true === $request['compute_totals'] ) {
return $this->create_refund_with_computed_totals( $request );
}
RestApiParameterUtil::adjust_create_refund_request_parameters( $request );
$order = wc_get_order( (int) $request['order_id'] );
if ( ! $order instanceof WC_Order ) {
return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 );
}
if ( 0 > $request['amount'] ) {
return new WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 );
}
$validation_error = $this->validate_explicit_refund_amounts( $order, $request['line_items'], $request['amount'] );
if ( is_wp_error( $validation_error ) ) {
return $validation_error;
}
$line_items = $request['line_items'];
if ( is_array( $line_items ) ) {
foreach ( $line_items as $item_id => $line ) {
if ( is_array( $line ) && ! isset( $line['refund_total'] ) && ! empty( $line['refund_tax'] ) ) {
$line_items[ $item_id ]['refund_total'] = 0;
}
}
}
// Create the refund.
$refund = wc_create_refund(
array(
'order_id' => $order->get_id(),
'amount' => $request['amount'],
'reason' => $request['reason'],
'line_items' => $line_items,
'refund_payment' => $request['api_refund'],
'restock_items' => $request['api_restock'],
)
);
if ( is_wp_error( $refund ) ) {
return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 );
}
if ( ! $refund ) {
return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 );
}
if ( ! empty( $request['meta_data'] ) ) {
MetaDataUtil::update( $request['meta_data'], $refund );
$refund->save_meta_data();
}
/**
* Filters an object before it is inserted via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`,
* refers to the object type slug.
*
* @param WC_Data $coupon Object object.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
*/
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating );
}