获取今天 Woocommerce 中每种产品的总订单数

Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
本文介绍了获取今天 Woocommerce 中每种产品的总订单数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一个代码片段来获取今天每种产品的总销售额,以便我可以在我的主题 functions.php 文件中使用它.

输出应该是这样的(每件商品的总销售额):

Product xxx = 25 个订单产品 yyy = 18 个订单产品 zzz = 8 个订单

解决方案

这可以通过以下非常简单的 SQL 查询和 foreach 循环来完成.

这将为您提供过去 24 小时内按产品计数的产品(和产品变体,但不是父变量产品)的产品列表:

全局$wpdb;$results = $wpdb->get_results( "SELECT DISTINCT woim.meta_value 作为id,COUNT(woi.order_id) 作为计数,woi.order_item_name 作为名称FROM {$wpdb->prefix}woocommerce_order_itemmeta as woimINNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_idINNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_idWHERE p.post_status IN ('wc-processing','wc-on-hold')AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400))AND ((woim.meta_key LIKE '_variation_id' AND woim.meta_value > 0)或 (woim.meta_key LIKE '_product_id'AND woim.meta_value NOT IN (SELECT DISTINCT post_parent FROM {$wpdb->prefix}posts WHERE post_type LIKE 'product_variation')))按 woim.meta_value 分组");//遍历每个产品foreach( $results 作为 $result ){$product_id = $result->id;$product_name = $result->name;$orders_count = $result->count;//格式化输出回声'产品:'.$product_name .'(' . $product_id . ') = ' .$orders_count .'
';}

经过测试并有效.


如果您想获得基于今天"的总数日期,您将在代码中替换这一行:

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

通过这一行:

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

<块引用>

时区调整使用CONVERT_TZ() SQL 函数
(您将在哪里调整 '+10:00' 最后一个参数作为偏移量以匹配时区)

AND DATE(p.post_date) >= DATE(CONVERT_TZ( NOW(),'+00:00','+10:00'))


相关类似答案:

  • 获取订单总购买金额Woocommerce 的一天
  • 总计数Woocommerce 上循环中的每个订单项

I'm looking for a code snippet to get total sales of each product for today, so I can use it in my theme functions.php file.

Output should be like this (Total Sales Per Item):

Product xxx = 25 orders
Product yyy = 18 orders
Product zzz = 8 orders

解决方案

This can be done with the following very light SQL query and a foreach loop.

That will give you the list of products (and product variations, but not parent variable products) of orders count by product for the past 24 hours:

global $wpdb;

$results = $wpdb->get_results( "
    SELECT DISTINCT woim.meta_value as id, COUNT(woi.order_id) as count, woi.order_item_name as name
    FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
    INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
    INNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_id
    WHERE p.post_status IN ('wc-processing','wc-on-hold')
    AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400))
    AND ((woim.meta_key LIKE '_variation_id' AND woim.meta_value > 0)
    OR (woim.meta_key LIKE '_product_id'
    AND woim.meta_value NOT IN (SELECT DISTINCT post_parent FROM {$wpdb->prefix}posts WHERE post_type LIKE 'product_variation')))
    GROUP BY woim.meta_value
" );

// Loop though each product
foreach( $results as $result ){
    $product_id   = $result->id;
    $product_name = $result->name;
    $orders_count = $result->count;
    
    // Formatted Output
    echo 'Product: ' . $product_name .' (' . $product_id . ') = ' . $orders_count . '<br>';
}

Tested and works.


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))

by this line:

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

Time zone ajustement using CONVERT_TZ() SQL function
(Where you will adjust '+10:00' the last argument as an offset to match the timezone)

AND DATE(p.post_date) >= DATE(CONVERT_TZ( NOW(),'+00:00','+10:00'))


Related similar answers:

  • Get orders total purchases amount for the day in Woocommerce
  • Total count for each order item in a loop on Woocommerce

这篇关于获取今天 Woocommerce 中每种产品的总订单数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)