WC_REST_Order_Refunds_Controller::preview_refundpublicWC 11.1.0

Preview a refund without creating it.

Returns server-computed refund totals and per-line breakdowns for the requested line items, using the same calculation engine as the wc/v4 refunds endpoints, so clients do not have to replicate tax, rounding, and currency-precision logic.

Метод класса: WC_REST_Order_Refunds_Controller{}

Возвращает

WP_REST_Response|WP_Error.

Использование

$WC_REST_Order_Refunds_Controller = new WC_REST_Order_Refunds_Controller();
$WC_REST_Order_Refunds_Controller->preview_refund( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Список изменений

С версии 11.1.0 Введена.

Код WC_REST_Order_Refunds_Controller::preview_refund() WC 11.1.1

public function preview_refund( $request ) {
	$order = wc_get_order( (int) $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 new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), array( 'status' => 404 ) );
	}

	// 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 and use unprefixed codes (the engine is convention neutral);
	// they are prefixed here at the v3 boundary so this endpoint follows the
	// `woocommerce_rest_*` convention of the rest of the v3 surface.
	$preview = $this->get_data_utils()->compute_refund_preview_or_error( $order, $request['line_items'], 'wc-rest-refunds' );

	if ( is_wp_error( $preview ) ) {
		return $this->prefix_error_code( $preview );
	}

	$preview = $this->add_preview_additional_fields( $preview, $request );

	$response = rest_ensure_response( $preview );

	/**
	 * Filters the refund preview response before it is returned, following the
	 * `woocommerce_rest_prepare_*` family contract. The preview is advisory:
	 * the create path re-validates independently, so filtered values cannot
	 * bypass the creation guards.
	 *
	 * @param WP_REST_Response $response The preview response. Its data carries
	 *                                   breakdown, subtotal, tax, total, max_refundable.
	 * @param WC_Order         $order    The order the refund preview was computed for.
	 * @param WP_REST_Request  $request  The request.
	 *
	 * @since 11.1.0
	 */
	return apply_filters( 'woocommerce_rest_prepare_order_refund_preview', $response, $order, $request );
}