Automattic\WooCommerce\Internal\RestApi\Routes\V4\Refunds

Controller::preview_itempublicWC 10.9.0

Preview a refund without creating it.

Method of the class: Controller{}

No Hooks.

Returns

WP_REST_Response|WP_Error.

Usage

$Controller = new Controller();
$Controller->preview_item( $request );
$request(required)
.

Changelog

Since 10.9.0 Introduced.

Controller::preview_item() code WC 11.1.1

public function preview_item( $request ) {
	$order = wc_get_order( $request['order_id'] );

	// wc_get_order returns WC_Order|WC_Order_Refund|false; only a WC_Order
	// (shop_order) is previewable here — refunds and missing IDs are rejected.
	if ( ! $order instanceof WC_Order ) {
		return $this->get_route_error_by_code( self::INVALID_ID );
	}

	// The shared engine runs the whole pipeline: normalize, validate, build,
	// and the aggregate guards. Its WP_Errors carry their HTTP status in the
	// error data; wrap them in this controller's error envelope, as the
	// validation branch always has.
	$preview = $this->data_utils->compute_refund_preview_or_error( $order, $request['line_items'], 'wc-v4-refunds' );

	if ( is_wp_error( $preview ) ) {
		$error_data = $preview->get_error_data();
		$status     = is_array( $error_data ) && isset( $error_data['status'] ) ? (int) $error_data['status'] : WP_Http::BAD_REQUEST;
		return $this->get_route_error_response_from_object( $preview, $status );
	}

	return rest_ensure_response( $preview );
}