获取Woocommerce当天订单的总购买金额 [英] Get orders total purchases amount for the day in Woocommerce

查看:210
本文介绍了获取Woocommerce当天订单的总购买金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Woocommerce,我在下面编写了代码,试图获取今天的订单总购买量:

For Woocommerce, I wrote code below, trying to get orders total purchases amount for today:

function order_total_woo_fahad(){

    // Get orders from people named John that were paid in the year 2016.
$orders = wc_get_orders( array(
    'date_paid' => '2018-07-03'
) );
$total_of_all=0;
for($i=0;$orders[i];$i++)
    $total_of_all= $orders[i]->get_total();
    return $total_of_all;
}

但是它返回null.

我做错了什么?如何获得当天的订单总购买金额?

What I am doing wrong? How can I get orders total purchases amount for the day?

推荐答案

获取此信息的最佳和有效方法是使用以下非常简单的SQL查询,该查询将获得最近24小时内所有订单总数的总和处理中"和已完成"订单状态:

The best and effective way to get that is to use the following very light SQL query, that will get the sum of all order totals in the last 24 hours for "processing" and "completed" orders statuses:

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')
        AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400))
        AND pm.meta_key LIKE '_order_total'
    " );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

用法示例-显示每日购买的格式化总金额:

USAGE Example - Display the daily total purchased formatted amount:

<?php echo '<p>Total purchased of the day: ' . strip_tags( wc_price(get_daily_purchases_total() ) ) . '</p>'; ?>


如果要改为基于今天"日期获取总数 ,则将以下行替换为代码:


If you want to get instead the total based on the "today" date, you will replace in the code this line:

AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400))

通过此行:

AND DATE(p.post_date) >= CURDATE()

这篇关于获取Woocommerce当天订单的总购买金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆