Abstract_WC_Order_Data_Store_CPT::get_batch_refund_totals │ protected │ WC 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).

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

Хуков нет.

Возвращает

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 Введена.

Код Abstract_WC_Order_Data_Store_CPT::get_batch_refund_totals() WC 11.2.2

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; trusted table names.
	$refund_totals = $wpdb->get_results(
		"SELECT posts.post_parent AS order_id, SUM( postmeta.meta_value ) AS total
			FROM {$wpdb->postmeta} AS postmeta
			INNER JOIN {$wpdb->posts} 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"
	);
	// 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;
}