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

查看:26
本文介绍了在 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;
}

但它返回空值.

我做错了什么?如何获取当天的订单总购买量?

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 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 = '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 = '_order_total'
    " );
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

USAGE Example - 显示每日总购买的格式化金额:

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天全站免登陆