Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_batch_refund_totals │ protected │ WC 10.7.0

Query total refunded amounts per order in a batch.

Overrides the CPT version to read directly from the HPOS orders table rather than joining postmeta.

Method of the class: OrdersTableDataStore{}

No Hooks.

Returns

array<int, float>. Map of order_id => refund total.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_batch_refund_totals( $order_ids ): array;
$order_ids(array) (required)
List of order IDs.

Changelog

Since 10.7.0 Introduced.

OrdersTableDataStore::get_batch_refund_totals() code WC 11.2.1

protected function get_batch_refund_totals( array $order_ids ): array {
	global $wpdb;

	$id_list      = implode( ', ', array_map( 'absint', $order_ids ) );
	$orders_table = self::get_orders_table_name();

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $id_list is sanitized via absint above; trusted table name.
	$refund_totals = $wpdb->get_results(
		"SELECT parent_order_id AS order_id, SUM( total_amount ) AS total
			FROM {$orders_table}
			WHERE type = 'shop_order_refund' AND parent_order_id IN ( $id_list )
			GROUP BY parent_order_id"
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

	$totals_by_order = array();
	foreach ( $refund_totals as $row ) {
		$totals_by_order[ $row->order_id ] = -1 * floatval( $row->total );
	}

	return $totals_by_order;
}