2017-10-04 237 views
1

我需要一個查詢,該查詢返回所有用戶的訂單並獲得批准付款。通過SQL查詢獲取所有WooCommerce客戶付款訂單

我很難找到db存儲在哪裏。我發現只有post_status'wc-complete',但我不認爲這是正確的信息。

SELECT a.post_status, b.meta_value FROM wp_posts a, wp_postmeta b 
    WHERE a.ID = b.post_id 
    AND a.post_type = 'shop_order' 
    AND a.post_status = 'wc-completed' 

回答

1

使用「完成」訂單狀態爲支付訂單是正確的

這裏是一個SQL查詢的自定義功能,這將輸出與他們完成的訂單用戶ID的格式陣列(支付/接受):

function completed_orders_ids_by_costumer_id(){ 

    global $wpdb; 

    $query = $wpdb->get_results(" 

     SELECT pm.meta_value AS user_id, pm.post_id AS order_id 
     FROM {$wpdb->prefix}postmeta AS pm 
     LEFT JOIN {$wpdb->prefix}posts AS p 
     ON pm.post_id = p.ID 
     WHERE p.post_type = 'shop_order' 
     AND p.post_status = 'wc-completed' 
     AND pm.meta_key = '_customer_user' 
     ORDER BY pm.meta_value ASC, pm.post_id DESC 
    "); 

    // We format the array by user ID 
    foreach($query as $result) 
     $results[$result->user_id][] = $result->order_id; 

    return $results; 
} 

代碼放在您的活動子主題(或主題的function.php文件),或者也可以任何插件文件。

經過測試和工作。


用法示例只是爲了測試,看看原始輸出數據

echo '<pre>'; print_r(completed_orders_ids_by_costumer_id()); echo '</pre>'; 

你會得到這樣的:在012

Array 
(
    [9] => Array 
     (
      [0] => 505 
      [0] => 497 
     ) 

    [12] => Array 
     (
      [0] => 626 
      [1] => 584 
      [2] => 483 
     ) 

    [15] => Array 
     (
      [0] => 614 
      [1] => 598 
     ) 

    [17] => Array 
     (
      [0] => 634 
     ) 

    …/… and so on … 

) 

現在表,你也會涉及到支付訂單其他一些元鍵,你可以使用:
'_paid_date'
'_date_paid'
'_date_completed'
'_completed_date'

但針對 「已完成」 的訂單狀態就好了

U唱別人上市Meta鍵,你可以讓你的SQL查詢(可以取代我們的函數首先查詢)

$query = $wpdb->get_results(" 
    SELECT DISTINCT pm.meta_value AS user_id, pm.post_id AS order_id 
    FROM {$wpdb->prefix}postmeta AS pm 
    LEFT JOIN {$wpdb->prefix}posts AS p 
    ON pm.post_id = p.ID 
    LEFT JOIN {$wpdb->prefix}postmeta AS pm2 
    ON p.ID = pm2.post_id 
    WHERE p.post_type = 'shop_order' 
    AND pm.meta_key = '_customer_user' 
    AND pm2.meta_key IN ('_paid_date', '_paid_date', '_date_completed', '_completed_date') 
    AND pm2.meta_value != '' 
    ORDER BY pm.meta_value ASC, pm.post_id DESC 
"); 

測試和工程太...

相關問題