How To Get Woocommerce Orders Total Sales Without Taxes?
So i want to calculate the total sale amount, excluding tax, for my website. However, i have a enormous load of orders on the website. Making it crash the page because it can't han
Solution 1:
You can use this custom function that uses a very lightweight SQL query using WordPress WPDB
Class to get orders total sales (excluding taxes).
It will get total sales from orders with "completed", "processing", "on-hold" and "pending" status.
The main function code:
functionget_orders_total_sales($type = 'excluding') {
global$wpdb;
// Excluding taxes (by default)if ( 'excluding' === $type ) {
$column = 'net_total';
}
// Including taxeselseif ( 'including' === $type ) {
$column = 'total_sales';
}
// only taxeselseif ( 'taxes' === $type ) {
$column = 'tax_total';
}
// only shippingelseif ( 'shipping' === $type ) {
$column = 'shipping_total';
}
return (float) $wpdb->get_var("
SELECT SUM($column)
FROM {$wpdb->prefix}wc_order_stats
WHERE status IN ('wc-completed','wc-processing','wc-on-hold','wc-pending')
");
}
Then you can use it in your own function like:
function calculateTotalSales(){
total_sales = get_orders_total_sales(); // get orders total sales (excluding taxes)
update_option( 'totalSales', total_sales ); // Save it as a setting optionreturn total_sales;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works in WooCommerce 4+.
The function also allow to get:
- orders total sales (including taxes):
get_orders_total_sales('including')
- orders total sales (only taxes):
get_orders_total_sales('taxes')
- orders total sales (only shipping):
get_orders_total_sales('shipping')
Post a Comment for "How To Get Woocommerce Orders Total Sales Without Taxes?"