获取 Woocommerce 中每种产品今天的总订单数
Get today's total orders count for each product in Woocommerce
我正在寻找一个代码片段来获取今天每种产品的总销售额,以便我可以在我的主题 functions.php 文件中使用它。
输出应该是这样的(每个项目的总销售额):
Product xxx = 25 orders
Product yyy = 18 orders
Product zzz = 8 orders
这可以通过以下非常简单的 SQL 查询和一个 foreach 循环来完成。
这将为您提供产品列表 (以及产品变体,但不是父变量产品) 过去 24 小时内按产品分类的订单数:
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>';
}
已测试并有效。
如果您想获取基于“今天”日期的总数,您将在代码中替换这一行:
AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400))
通过这条线:
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'))
相关相似回答:
我正在寻找一个代码片段来获取今天每种产品的总销售额,以便我可以在我的主题 functions.php 文件中使用它。
输出应该是这样的(每个项目的总销售额):
Product xxx = 25 orders
Product yyy = 18 orders
Product zzz = 8 orders
这可以通过以下非常简单的 SQL 查询和一个 foreach 循环来完成。
这将为您提供产品列表 (以及产品变体,但不是父变量产品) 过去 24 小时内按产品分类的订单数:
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>';
}
已测试并有效。
如果您想获取基于“今天”日期的总数,您将在代码中替换这一行:
AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - (86400))
通过这条线:
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'))
相关相似回答: