Get orders total purchases amount for the day in Woocommerce

后端 未结 2 1352
死守一世寂寞
死守一世寂寞 2020-12-01 20:37

For Woocommerce, I wrote code below, trying to get orders total purchases amount for today:

function order_total_woo_fahad(){

    // Get orders from people          


        
相关标签:
2条回答
  • 2020-12-01 21:04

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

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


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

    by this line:

    AND DATE(p.post_date) >= CURDATE()
    
    0 讨论(0)
  • 2020-12-01 21:13
    <?php
    global $wpdb;
    
    $date_from = '2015-11-20';
    $date_to = '2015-12-20';
    $post_status = implode("','", array('wc-processing', 'wc-completed') );
    
    $result = $wpdb->get_results( "SELECT * FROM $wpdb->posts 
                WHERE post_type = 'shop_order'
                AND post_status IN ('{$post_status}')
                AND post_date BETWEEN '{$date_from}  00:00:00' AND '{$date_to} 23:59:59'
            ");
    
    echo "<pre>";
    print_r($result);
    ?>
    
    0 讨论(0)
提交回复
热议问题