WordPress at Your Fingertips
function is not described

Automattic\WooCommerce\Internal

RestApiUtil::calculate_refund_amount_from_line_items() private WC 1.0

Calculate the "amount" parameter for the request based on the amounts found in line items. This will ONLY be possible if ALL of the following is true:

  • "line_items" in the request is a non-empty array.
  • All line items have a "refund_total" field with a numeric value.
  • All values inside "refund_tax" in all line items are a numeric value.

The request is assumed to be in internal format already.

{} It's a method of the class: RestApiUtil{}

No Hooks.

Return

number|null. The calculated amount, or null if it can't be calculated.

Usage

$result = RestApiUtil::calculate_refund_amount_from_line_items( $request );
$request(\WP_REST_Request) (required)
The request to maybe calculate the total amount for.

Code of RestApiUtil::calculate_refund_amount_from_line_items() WC 5.9.0

private static function calculate_refund_amount_from_line_items( $request ) {
	$line_items = $request['line_items'];

	if ( ! is_array( $line_items ) || empty( $line_items ) ) {
		return null;
	}

	$amount = 0;

	foreach ( $line_items as $item ) {
		if ( ! isset( $item['refund_total'] ) || ! is_numeric( $item['refund_total'] ) ) {
			return null;
		}

		$amount += $item['refund_total'];

		if ( ! isset( $item['refund_tax'] ) ) {
			continue;
		}

		foreach ( $item['refund_tax'] as $tax ) {
			if ( ! is_numeric( $tax ) ) {
				return null;
			}
			$amount += $tax;
		}
	}

	return $amount;
}