我当前的职能部门成功查询了我网站上的全部销售额。但是我不确定如何仅对当前登录系统的用户限制数量。我的意思是说他们总共购买了。他们将能够看到他们购买的总额。下面的当前代码将显示当天的总销售额。
我的代码
function get_daily_purchases_total(){
global $wpdb;
return $wpdb->get_var( "
SELECT DISTINCT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-processing','wc-completed', 'wc-on-hold')
AND DATE(p.post_date) >= CURDATE()
AND pm.meta_key LIKE '_order_total'
" );
}
答案 0 :(得分:1)
WooCommerce本身具有wc_get_customer_total_spent()的内置函数,其中user_id是参数,但是根据您的要求,我对WooCommerce所使用的查询进行了少许修改
function so_55556214_get_orders_from_user_id() {
$user_id = get_current_user_id();
if ($user_id > 0) {
//run only for members
$statuses = array('completed', 'processing', 'on-hold');
global $wpdb;
$result = $wpdb->get_var("SELECT SUM(meta2.meta_value)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = '" . esc_sql($user_id) . "'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( 'wc-" . implode("','wc-", $statuses) . "' )
AND meta2.meta_key = '_order_total'
AND DATE(posts.post_date) >= CURDATE() ");
return $result;
}
}
我在我这边测试了上面的代码,看来工作正常。希望对您有所帮助:-)