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.

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

Хуков нет.

Возвращает

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

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_batch_refund_totals( $order_ids ): array;
$order_ids(массив) (обязательный)
List of order IDs.

Список изменений

С версии 10.7.0 Введена.

Код OrdersTableDataStore::get_batch_refund_totals() WC 11.1.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 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;
}