Abstract_WC_Order_Data_Store_CPT::get_batch_refund_totalsprotectedWC 10.7.0

Query total refunded amounts per order in a batch. Returns an associative array of order_id => total (positive float).

Subclasses should override this when the refund total is stored differently (e.g. HPOS stores it directly in the orders table rather than postmeta).

Method of the class: Abstract_WC_Order_Data_Store_CPT{}

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.

Abstract_WC_Order_Data_Store_CPT::get_batch_refund_totals() code WC 10.8.1

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 posts.post_parent AS order_id, SUM( postmeta.meta_value ) AS total
			FROM %i AS postmeta
			INNER JOIN %i AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent IN ( $id_list ) )
			WHERE postmeta.meta_key = '_refund_amount'
			AND postmeta.post_id = posts.ID
			GROUP BY posts.post_parent",
			$wpdb->postmeta,
			$wpdb->posts
		)
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

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

	return $totals_by_order;
}