Automattic\WooCommerce\Internal

RestApiUtil::adjust_create_refund_request_parameters()public staticWC 1.0

Converts a create refund request from the public API format:

[ "reason" => "", "api_refund" => "x", "api_restock" => "x", "line_items" => [

"id" => "111",
"quantity" => 222,
"refund_total" => 333,
"refund_tax" => [
  [
	 "id": "444",
	 "refund_total": 555
  ],...

],... ]

...to the internally used format:

[ "reason" => null, (if it's missing or any empty value, set as null) "api_refund" => true, (if it's missing or non-bool, set as "true") "api_restock" => true, (if it's missing or non-bool, set as "true") "line_items" => [ (convert sequential array to associative based on "id")

"111" => [
  "qty" => 222,      (rename "quantity" to "qty")
  "refund_total" => 333,
  "refund_tax" => [  (convert sequential array to associative based on "id" and "refund_total)
	"444" => 555,...
  ],...

] ]

It also calculates the amount if missing and whenever possible, see maybe_calculate_refund_amount_from_line_items.

The conversion is done in a way that if the request is already in the internal format, then nothing is changed for compatibility. For example, if the line items array is already an associative array or any of its elements is missing the "id" key, then the entire array is left unchanged. Same for the "refund_tax" array inside each line item.

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

Хуков нет.

Возвращает

null. Ничего (null).

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

$result = RestApiUtil::adjust_create_refund_request_parameters( $request );
$request(\WP_REST_Request) (обязательный)
The request to adjust.

Код RestApiUtil::adjust_create_refund_request_parameters() WC 8.7.0

public static function adjust_create_refund_request_parameters( \WP_REST_Request &$request ) {
	if ( empty( $request['reason'] ) ) {
		$request['reason'] = null;
	}

	if ( ! is_bool( $request['api_refund'] ) ) {
		$request['api_refund'] = true;
	}

	if ( ! is_bool( $request['api_restock'] ) ) {
		$request['api_restock'] = true;
	}

	if ( empty( $request['line_items'] ) ) {
		$request['line_items'] = array();
	} else {
		$request['line_items'] = self::adjust_line_items_for_create_refund_request( $request['line_items'] );
	}

	if ( ! isset( $request['amount'] ) ) {
		$amount = self::calculate_refund_amount_from_line_items( $request );
		if ( null !== $amount ) {
			$request['amount'] = strval( $amount );
		}
	}
}