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

DataUtils::calculate_refund_amountpublicWC 1.0

Calculate the gross refund amount from line items (schema format).

Sums refund_total plus any explicit refund_tax. This yields the tax-inclusive gross for both forms: when refund_tax is omitted, refund_total is already tax-inclusive (and there is no refund_tax to add); when refund_tax is supplied, refund_total is the tax-exclusive subtotal and the taxes are added on top.

Method of the class: DataUtils{}

No Hooks.

Returns

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

Usage

$DataUtils = new DataUtils();
$DataUtils->calculate_refund_amount( $line_items ): ?float;
$line_items(array) (required)
The line items to calculate the refund amount from.

DataUtils::calculate_refund_amount() code WC 11.0.1

public function calculate_refund_amount( array $line_items ): ?float {
	if ( empty( $line_items ) ) {
		return null;
	}

	$amount = 0;

	foreach ( $line_items as $line_item ) {
		// is_numeric() (not !empty) — an explicit refund_total of 0 can be part
		// of a valid tax-only refund and must be included in the gross sum.
		if ( isset( $line_item['refund_total'] ) && is_numeric( $line_item['refund_total'] ) ) {
			$amount += $line_item['refund_total'];
		}

		if ( ! empty( $line_item['refund_tax'] ) && is_array( $line_item['refund_tax'] ) ) {
			foreach ( $line_item['refund_tax'] as $tax ) {
				if ( isset( $tax['refund_total'] ) && is_numeric( $tax['refund_total'] ) ) {
					$amount += $tax['refund_total'];
				}
			}
		}
	}

	return (float) NumberUtil::round( $amount, wc_get_price_decimals() );
}