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

DataUtils::convert_line_items_to_internal_formatpublicWC 1.0

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,

], ]

Метод класса: DataUtils{}

Хуков нет.

Возвращает

Массив. The converted line items.

Использование

$DataUtils = new DataUtils();
$DataUtils->convert_line_items_to_internal_format( $line_items, $order );
$line_items(массив) (обязательный)
The line items to convert.
$order(WC_Order) (обязательный)
The order being refunded.

Код DataUtils::convert_line_items_to_internal_format() WC 10.5.2

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();
				$tax_ids        = array_keys( $original_taxes['total'] ?? array() );

				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
					);

					$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'] = $line_item['refund_total'] - $total_tax;
				}
			}
		}

		$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;
}