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

DataUtils::compute_line_item_refund_totalpublicWC 10.9.0

Compute the tax-inclusive refund total for a line item at a given quantity.

Precondition: $item must be one of WC_Order_Item_Product, WC_Order_Item_Shipping, WC_Order_Item_Fee, and $quantity must be a positive integer (>= 1). For shipping and fee items the quantity is informational only — the full item total is returned regardless. Callers using untrusted input should validate via {@see validate_preview_line_items()} first.

Method of the class: DataUtils{}

No Hooks.

Returns

float. The tax-inclusive refund total. May be negative for items with negative totals (e.g. discount fees).

Usage

$DataUtils = new DataUtils();
$DataUtils->compute_line_item_refund_total( $item, $quantity ): float;
$item(WC_Order_Item_Product|WC_Order_Item_Shipping|WC_Order_Item_Fee) (required)
The order item.
$quantity(int) (required)
The quantity to refund (>= 1).

Changelog

Since 10.9.0 Introduced.

DataUtils::compute_line_item_refund_total() code WC 11.0.1

public function compute_line_item_refund_total( $item, int $quantity ): float {
	if ( $quantity < 1 ) {
		// Exception message is developer-facing only; the value is a typed int and the format is a literal string.
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
		throw new \InvalidArgumentException( sprintf( 'Quantity must be >= 1, got %d.', (int) $quantity ) );
	}

	$price_decimals = wc_get_price_decimals();

	if ( $item instanceof WC_Order_Item_Product ) {
		$original_qty = $item->get_quantity();
		if ( 0 === $original_qty ) {
			wc_get_logger()->warning(
				sprintf( 'Refund preview: product item %d has zero original quantity on order %d.', $item->get_id(), $item->get_order_id() ),
				array( 'source' => 'wc-v4-refunds' )
			);
			return 0.0;
		}
		$unit_price_with_tax = ( (float) $item->get_total() + (float) $item->get_total_tax() ) / $original_qty;
		return NumberUtil::round( $unit_price_with_tax * $quantity, $price_decimals );
	}

	return NumberUtil::round( (float) $item->get_total() + (float) $item->get_total_tax(), $price_decimals );
}