Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_batch_refund_totalsprotectedWC 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. 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 10.7.0

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

	$id_list = implode( ', ', array_map( 'absint', $order_ids ) );

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $id_list is sanitized via absint above.
	$refund_totals = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT parent_order_id AS order_id, SUM( total_amount ) AS total
			FROM %i
			WHERE type = 'shop_order_refund' AND parent_order_id IN ( $id_list )
			GROUP BY parent_order_id",
			self::get_orders_table_name()
		)
	);
	// 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;
}