久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取今天 Woocommerce 中每種產品的總訂單數

Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
本文介紹了獲取今天 Woocommerce 中每種產品的總訂單數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在尋找一個代碼片段來獲取今天每種產品的總銷售額,以便我可以在我的主題 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 中每種產品的總訂單數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
Add birthday field to WooCommerce my account and admin user page(將生日字段添加到 WooCommerce 我的帳戶和管理員用戶頁面)
主站蜘蛛池模板: 中文字幕成人 | 99精品欧美一区二区蜜桃免费 | 中文字幕一区在线观看视频 | 日韩高清黄色 | 日本在线视频中文字幕 | 国产探花在线精品一区二区 | 91国内外精品自在线播放 | 中文字幕高清一区 | 久久国产精品一区二区 | 中文字幕亚洲一区二区三区 | 欧美片网站免费 | 色av一区二区 | 亚洲婷婷六月天 | 一级毛片免费 | 精品国产伦一区二区三区观看方式 | www.中文字幕av| 在线观看精品视频网站 | 亚洲欧洲在线视频 | 黄篇网址 | 成人av一区 | 国产精品久久精品 | 国产在线精品一区二区三区 | 国产高清在线精品一区二区三区 | 午夜国产一级 | 91av视频在线免费观看 | 四季久久免费一区二区三区四区 | 中文字幕视频在线观看 | 精品国产精品三级精品av网址 | 久久国产精品视频 | 国产精品久久久久国产a级 欧美日本韩国一区二区 | 日韩精品视频在线观看一区二区三区 | 亚洲精品一二三区 | 91精品国产欧美一区二区成人 | 亚洲欧美一区二区三区视频 | 国产精华一区 | 国产我和子的乱视频网站 | 午夜性色a√在线视频观看9 | 亚洲一区二区av | 三级黄色片在线观看 | 伦理二区 | 国产精品久久久久久久久久久新郎 |