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:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…