Automattic\WooCommerce\Internal\RestApi\Routes\V4\Refunds
DataUtils::convert_line_items_to_internal_format
Convert line items (schema format) to internal format. This keys arrays by item ID and has some different naming conventions.
111 => [ "qty" => 1, "refund_total" => 123, "refund_tax" => [
1 => 123, 2 => 456,
], ]
Method of the class: DataUtils{}
No Hooks.
Returns
Array. The converted line items.
Usage
$DataUtils = new DataUtils(); $DataUtils->convert_line_items_to_internal_format( $line_items, $order );
- $line_items(array) (required)
- The line items to convert.
- $order(WC_Order) (required)
- The order being refunded.
DataUtils::convert_line_items_to_internal_format() DataUtils::convert line items to internal format code WC 10.9.4
public function convert_line_items_to_internal_format( $line_items, WC_Order $order ) {
$prepared_line_items = array();
foreach ( $line_items as $line_item ) {
if ( ! isset( $line_item['line_item_id'], $line_item['quantity'], $line_item['refund_total'] ) ) {
continue;
}
// If no explicit refund_tax provided, extract tax from refund_total using WC_Tax.
if ( ! isset( $line_item['refund_tax'] ) ) {
$original_item = $order->get_item( $line_item['line_item_id'] );
if ( $original_item ) {
$original_taxes = $original_item->get_taxes();
// Filter to only include tax IDs that have non-zero amounts.
$tax_totals = array_filter(
$original_taxes['total'] ?? array(),
function ( $amount ) {
return is_numeric( $amount ) && $amount > 0;
}
);
$tax_ids = array_keys( $tax_totals );
if ( ! empty( $tax_ids ) ) {
$tax_rates = $this->build_tax_rates_array( $order, $tax_ids );
// Always assume refund_total includes tax - extract it using WC_Tax.
$calculated_taxes = WC_Tax::calc_inclusive_tax(
(float) $line_item['refund_total'],
$tax_rates
);
// Round extracted taxes to display precision to match how original taxes were stored.
// This prevents rounding errors where internal precision (6DP) differs from storage precision (2DP).
$price_decimals = wc_get_price_decimals();
$calculated_taxes = array_map(
function ( $tax ) use ( $price_decimals ) {
return NumberUtil::round( $tax, $price_decimals );
},
$calculated_taxes
);
$line_item['refund_tax'] = $this->convert_proportional_taxes_to_schema_format(
$calculated_taxes
);
// Subtract extracted tax from refund_total to get the amount excluding tax.
$total_tax = array_sum( $calculated_taxes );
$line_item['refund_total'] = NumberUtil::round( $line_item['refund_total'] - $total_tax, $price_decimals );
}
}
}
$prepared_line_items[ $line_item['line_item_id'] ] = array(
'qty' => $line_item['quantity'],
'refund_total' => $line_item['refund_total'],
'refund_tax' => $this->convert_line_item_taxes_to_internal_format( $line_item['refund_tax'] ?? array() ),
);
}
return $prepared_line_items;
}