Php 如何获得WooCommerce订单无税总销售额?
所以我想计算我的网站的总销售额,不含税。然而,我在网站上有大量的订单。使其崩溃页面,因为它无法处理计算。是否有更好的方法从WooCommerce计算/检索此信息Php 如何获得WooCommerce订单无税总销售额?,php,sql,wordpress,woocommerce,orders,Php,Sql,Wordpress,Woocommerce,Orders,所以我想计算我的网站的总销售额,不含税。然而,我在网站上有大量的订单。使其崩溃页面,因为它无法处理计算。是否有更好的方法从WooCommerce计算/检索此信息 function calculateTotalSales(){ $orders = get_posts( array( 'numberposts' => - 1, 'post_type' => array( 'shop_order' ), 'post_status
function calculateTotalSales(){
$orders = get_posts( array(
'numberposts' => - 1,
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed', 'wc-processing', 'wc-pending' )
) );
$total = 0;
foreach ( $orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total() - $order->get_total_tax();
}
update_option('totalSales', $totalSales);
return $totalSales;
}
您可以使用这个自定义函数,它使用一个非常轻量级的SQL查询,使用WordPress获取订单总销售额(不含税) 它将从状态为“已完成”、“正在处理”、“暂停”和“待定”的订单中获取总销售额 主要功能代码:
function get_orders_total_sales( $type = 'excluding' ) {
global $wpdb;
// Excluding taxes (by default)
if ( 'excluding' === $type ) {
$column = 'net_total';
}
// Including taxes
elseif ( 'including' === $type ) {
$column = 'total_sales';
}
// only taxes
elseif ( 'taxes' === $type ) {
$column = 'tax_total';
}
// only shipping
elseif ( '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')
");
}
然后,您可以在自己的功能中使用它,如:
function calculateTotalSales(){
total_sales = get_orders_total_sales(); // get orders total sales (excluding taxes)
update_option( 'totalSales', total_sales ); // Save it as a setting option
return total_sales;
}
代码进入活动子主题(或活动主题)的functions.php文件。在WooCommerce 4+中测试并工作
该函数还允许获取:
- 订单总销售额(包括税费):
get\u orders\u total\u sales('include')
- 订单总销售额(仅税):
get\u orders\u total\u sales(“税”)
- 订单总销售额(仅发货):
获取订单总销售额(“发货”)