在 Woocommerce 中显示客户在没有运送的情况下花费的总金额
Display Customer total spent without shipping in Woocommerce
我正在尝试显示每个客户在不包括运费的情况下总共花费了多少钱。以下代码中的所有内容(创建一个新列并使其可排序)都可以正常工作,但计算除外。
我在第 37 行收到错误:
$money_spent = wc_get_customer_total_spent( $user_id ) - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' );
这是我使用的所有代码=>
class Total_Spent_By_Customer_WooCommerce {
public function __construct() {
add_action( 'init', array( &$this, 'init' ) );
}
public function init() {
add_filter( 'manage_users_columns', array( $this,'users_columns') );
add_action( 'manage_users_custom_column', array( $this ,'users_custom_column'), 10, 3);
add_filter( 'manage_users_sortable_columns', array( $this ,'users_sortable_columns') );
add_filter( 'users_list_table_query_args', array( $this ,'users_orderby_column'), 10, 1 );
add_action( 'plugins_loaded', array( $this ,'load_this_textdomain') );
}
public static function users_columns( $columns ) {
unset($columns['posts']);
$columns['money_spent'] = _x( 'Money Spent', 'user', 'total-spent-by-customer-for-woocommerce' );
return $columns;
}
public static function users_custom_column( $value, $column_name, $user_id ) {
if ( 'money_spent' != $column_name ) {
return $value;
} else {
$money_spent = wc_get_customer_total_spent( $user_id ) - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' );
return wc_price( wc_get_customer_total_spent ( $user_id ));
}
}
public static function users_sortable_columns($columns) {
$custom = array(
'money_spent' => 'money_spent',
);
return wp_parse_args( $custom, $columns );
}
public static function users_orderby_column( $args ) {
if ( isset( $args['orderby'] ) && 'money_spent' == $args['orderby'] ) {
$args = array_merge( $args, array(
'meta_key' => '_money_spent',
'orderby' => 'meta_value_num',
));
}
return $args;
}
public function load_this_textdomain() {
load_plugin_textdomain( 'total-spent-by-customer-for-woocommerce' );
}
}
new Total_Spent_By_Customer_WooCommerce();
如果能为此提供任何类型的帮助,我将不胜感激。
更新 2
使用 wc_get_customer_total_spent( $user_id )
函数,您 无法在没有运费和税金的情况下获得客户总支出 ,因为您应该需要删除每个订单的税金和运费当前客户已取得。
您也无法在函数中获取和使用 WC_Order
对象。所以WC_Order
方法get_total_tax()
、get_total_shipping()
和get_shipping_tax()
不能使用。
另一种方法 可以让客户在不含运费和税金的情况下花费总金额:
基于 the SQL Woocommerce query source code 涉及的 wc_get_customer_total_spent()
函数,我们可以构建您自己的查询。
所以你将用这个替换你的函数:
public static function users_custom_column( $value, $column_name, $user_id ) {
if ( 'money_spent' == $column_name ) {
global $wpdb;
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$spent = $wpdb->get_var("
SELECT SUM(pm2.meta_value - (pm3.meta_value + pm4.meta_value))
FROM $wpdb->posts as p
LEFT JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
LEFT JOIN {$wpdb->postmeta} AS pm2 ON p.ID = pm2.post_id
LEFT JOIN {$wpdb->postmeta} AS pm3 ON p.ID = pm3.post_id
LEFT JOIN {$wpdb->postmeta} AS pm4 ON p.ID = pm4.post_id
WHERE pm.meta_key = '_customer_user'
AND pm.meta_value = '" . esc_sql( $user_id ) . "'
AND p.post_type = 'shop_order'
AND p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
AND pm2.meta_key = '_order_total'
AND pm3.meta_key = '_order_tax'
AND pm4.meta_key = '_order_shipping'
");
if ( ! $spent ) {
$spent = 0;
}
$value = wc_price( $spent );
}
return $value;
}
经过测试,完美运行。
Calculations note:
As you are removing the total taxes from the orders you don't need to remove the shipping taxes (as they are included in the total taxes).
So we just remove the total taxes and the total shipping (excluding taxes).
我正在尝试显示每个客户在不包括运费的情况下总共花费了多少钱。以下代码中的所有内容(创建一个新列并使其可排序)都可以正常工作,但计算除外。
我在第 37 行收到错误:
$money_spent = wc_get_customer_total_spent( $user_id ) - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' );
这是我使用的所有代码=>
class Total_Spent_By_Customer_WooCommerce {
public function __construct() {
add_action( 'init', array( &$this, 'init' ) );
}
public function init() {
add_filter( 'manage_users_columns', array( $this,'users_columns') );
add_action( 'manage_users_custom_column', array( $this ,'users_custom_column'), 10, 3);
add_filter( 'manage_users_sortable_columns', array( $this ,'users_sortable_columns') );
add_filter( 'users_list_table_query_args', array( $this ,'users_orderby_column'), 10, 1 );
add_action( 'plugins_loaded', array( $this ,'load_this_textdomain') );
}
public static function users_columns( $columns ) {
unset($columns['posts']);
$columns['money_spent'] = _x( 'Money Spent', 'user', 'total-spent-by-customer-for-woocommerce' );
return $columns;
}
public static function users_custom_column( $value, $column_name, $user_id ) {
if ( 'money_spent' != $column_name ) {
return $value;
} else {
$money_spent = wc_get_customer_total_spent( $user_id ) - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax(), wc_get_price_decimals(), '.', '' );
return wc_price( wc_get_customer_total_spent ( $user_id ));
}
}
public static function users_sortable_columns($columns) {
$custom = array(
'money_spent' => 'money_spent',
);
return wp_parse_args( $custom, $columns );
}
public static function users_orderby_column( $args ) {
if ( isset( $args['orderby'] ) && 'money_spent' == $args['orderby'] ) {
$args = array_merge( $args, array(
'meta_key' => '_money_spent',
'orderby' => 'meta_value_num',
));
}
return $args;
}
public function load_this_textdomain() {
load_plugin_textdomain( 'total-spent-by-customer-for-woocommerce' );
}
}
new Total_Spent_By_Customer_WooCommerce();
如果能为此提供任何类型的帮助,我将不胜感激。
更新 2
使用 wc_get_customer_total_spent( $user_id )
函数,您 无法在没有运费和税金的情况下获得客户总支出 ,因为您应该需要删除每个订单的税金和运费当前客户已取得。
您也无法在函数中获取和使用 WC_Order
对象。所以WC_Order
方法get_total_tax()
、get_total_shipping()
和get_shipping_tax()
不能使用。
另一种方法 可以让客户在不含运费和税金的情况下花费总金额:
基于 the SQL Woocommerce query source code 涉及的 wc_get_customer_total_spent()
函数,我们可以构建您自己的查询。
所以你将用这个替换你的函数:
public static function users_custom_column( $value, $column_name, $user_id ) {
if ( 'money_spent' == $column_name ) {
global $wpdb;
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$spent = $wpdb->get_var("
SELECT SUM(pm2.meta_value - (pm3.meta_value + pm4.meta_value))
FROM $wpdb->posts as p
LEFT JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
LEFT JOIN {$wpdb->postmeta} AS pm2 ON p.ID = pm2.post_id
LEFT JOIN {$wpdb->postmeta} AS pm3 ON p.ID = pm3.post_id
LEFT JOIN {$wpdb->postmeta} AS pm4 ON p.ID = pm4.post_id
WHERE pm.meta_key = '_customer_user'
AND pm.meta_value = '" . esc_sql( $user_id ) . "'
AND p.post_type = 'shop_order'
AND p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
AND pm2.meta_key = '_order_total'
AND pm3.meta_key = '_order_tax'
AND pm4.meta_key = '_order_shipping'
");
if ( ! $spent ) {
$spent = 0;
}
$value = wc_price( $spent );
}
return $value;
}
经过测试,完美运行。
Calculations note:
As you are removing the total taxes from the orders you don't need to remove the shipping taxes (as they are included in the total taxes).
So we just remove the total taxes and the total shipping (excluding taxes).