WC_Tracker::get_order_totals()private staticWC 5.4.0

Get order totals.

Method of the class: WC_Tracker{}

No Hooks.

Return

Array.

Usage

$result = WC_Tracker::get_order_totals();

Changelog

Since 5.4.0 Introduced.

WC_Tracker::get_order_totals() code WC 8.9.0

private static function get_order_totals() {
	global $wpdb;

	$orders_table = OrdersTableDataStore::get_orders_table_name();

	if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$gross_total = $wpdb->get_var(
			"
			SELECT SUM(total_amount) AS 'gross_total'
			FROM $orders_table
			WHERE status in ('wc-completed', 'wc-refunded');
		"
		);
		// phpcs:enable
	} else {
		$gross_total = $wpdb->get_var(
			"
				SELECT
					SUM( order_meta.meta_value ) AS 'gross_total'
				FROM {$wpdb->prefix}posts AS orders
				LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID
				WHERE order_meta.meta_key = '_order_total'
					AND orders.post_status in ( 'wc-completed', 'wc-refunded' )
				GROUP BY order_meta.meta_key
			"
		);
	}

	if ( is_null( $gross_total ) ) {
		$gross_total = 0;
	}

	if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$processing_gross_total = $wpdb->get_var(
			"
			SELECT SUM(total_amount) AS 'gross_total'
			FROM $orders_table
			WHERE status = 'wc-processing';
		"
		);
		// phpcs:enable
	} else {
		$processing_gross_total = $wpdb->get_var(
			"
			SELECT
				SUM( order_meta.meta_value ) AS 'gross_total'
			FROM {$wpdb->prefix}posts AS orders
			LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID
			WHERE order_meta.meta_key = '_order_total'
				AND orders.post_status = 'wc-processing'
			GROUP BY order_meta.meta_key
		"
		);
	}

	if ( is_null( $processing_gross_total ) ) {
		$processing_gross_total = 0;
	}

	return array(
		'gross'            => $gross_total,
		'processing_gross' => $processing_gross_total,
	);
}