WC_Tracker::get_order_totals
Get order totals.
Keeping the internal statuses names as strings to avoid regression issues (not referencing Automattic\WooCommerce\Enums\OrderInternalStatus class).
Method of the class: WC_Tracker{}
No Hooks.
Returns
Array.
Usage
$result = WC_Tracker::get_order_totals();
Changelog
| Since 5.4.0 | Introduced. |
WC_Tracker::get_order_totals() WC Tracker::get order totals code WC 10.5.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,
);
}