WC_REST_Order_Refunds_Controller::normalize_line_item
Normalize one compute_totals line item to the shared engine's shape.
Maps the create endpoint's public id key to the engine's line_item_id and validates/normalizes the scalar types. The REST schema cannot validate the line_items subtree (the property is readonly for backward compatibility), so without this check malformed values such as an array refund_total would reach the calculation engine and fail with a TypeError instead of a 400 response. Uses the same error codes as the engine's own validation, and casts numeric strings to their proper types.
Метод класса: WC_REST_Order_Refunds_Controller{}
Хуков нет.
Возвращает
array|WP_Error. The normalized line item, or WP_Error on an invalid type.
Использование
// private - только в коде основоного (родительского) класса $result = $this->normalize_line_item( $line_item );
- $line_item(массив) (обязательный)
- Line item in the public request shape (id keys).
Список изменений
| С версии 11.1.0 | Введена. |
Код WC_REST_Order_Refunds_Controller::normalize_line_item() WC REST Order Refunds Controller::normalize line item WC 11.1.1
private function normalize_line_item( array $line_item ) {
// The create endpoint documents `id`; the shared engine and the preview
// endpoint key lines by `line_item_id`, and either form is accepted here.
// A payload carrying both is rejected: silently preferring one could
// refund and restock a different line than the client intended.
if ( isset( $line_item['id'], $line_item['line_item_id'] ) ) {
return new WP_Error( 'woocommerce_rest_invalid_line_item', __( 'Specify the line item with either id or line_item_id, not both.', 'woocommerce' ), array( 'status' => 400 ) );
}
if ( isset( $line_item['id'] ) ) {
$line_item['line_item_id'] = $line_item['id'];
unset( $line_item['id'] );
}
// IDs must be whole numbers (rest_is_integer): silently truncating a
// fractional id such as 123.5 to 123 would target a different line or
// tax bucket than requested.
if ( isset( $line_item['line_item_id'] ) ) {
if ( ! rest_is_integer( $line_item['line_item_id'] ) ) {
return new WP_Error( 'woocommerce_rest_invalid_line_item', __( 'Line item id must be an integer.', 'woocommerce' ), array( 'status' => 400 ) );
}
$line_item['line_item_id'] = (int) $line_item['line_item_id'];
}
if ( isset( $line_item['quantity'] ) ) {
if ( ! rest_is_integer( $line_item['quantity'] ) ) {
return new WP_Error( 'woocommerce_rest_invalid_quantity', __( 'Quantity must be a whole number.', 'woocommerce' ), array( 'status' => 400 ) );
}
$line_item['quantity'] = (int) $line_item['quantity'];
}
if ( isset( $line_item['refund_total'] ) ) {
if ( ! is_numeric( $line_item['refund_total'] ) ) {
return new WP_Error( 'woocommerce_rest_invalid_refund_total', __( 'refund_total must be a number.', 'woocommerce' ), array( 'status' => 400 ) );
}
$line_item['refund_total'] = (float) $line_item['refund_total'];
}
if ( isset( $line_item['refund_tax'] ) ) {
if ( ! is_array( $line_item['refund_tax'] ) ) {
return new WP_Error( 'woocommerce_rest_invalid_line_item', __( 'refund_tax must be an array of objects with id and refund_total.', 'woocommerce' ), array( 'status' => 400 ) );
}
foreach ( $line_item['refund_tax'] as $index => $tax ) {
if ( ! is_array( $tax ) || ! isset( $tax['id'], $tax['refund_total'] ) || ! rest_is_integer( $tax['id'] ) || ! is_numeric( $tax['refund_total'] ) ) {
return new WP_Error( 'woocommerce_rest_invalid_line_item', __( 'refund_tax entries must be objects with an integer id and a numeric refund_total.', 'woocommerce' ), array( 'status' => 400 ) );
}
$line_item['refund_tax'][ $index ] = array(
'id' => (int) $tax['id'],
'refund_total' => (float) $tax['refund_total'],
);
}
}
return $line_item;
}