Total order quantity and total sales
-
Hello, I am trying to create the code that shows the total number of orders and total sales amount in woocommerce, but when I run the code below, I cannot get the correct result.
What I want is to show all order numbers and total amounts created on the orders page. However, this code continues to show even if I delete an order, and the total sales amount is not correct. So, on the orders page, I need to show the number and total amount of orders that have not been deleted.
function wc_get_order_count() {
$args = array(
'post_type' => 'shop_order',
'post_status' => array_keys(wc_get_order_statuses()),
'numberposts' => -1,
);
$orders = get_posts($args);
return count($orders);
}
function wc_get_total_sales() {
global $wpdb;
$total_sales = $wpdb->get_var("
SELECT SUM(meta.meta_value)
FROM {$wpdb->prefix}postmeta AS meta
LEFT JOIN {$wpdb->prefix}posts AS posts ON meta.post_id = posts.ID
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
");
return $total_sales;
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- You must be logged in to reply to this topic.