Advertisement
fly2high

Top Customer Order List

Jun 23rd, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. global $wpdb;
  2.  
  3. $sql = "";
  4. $sql .= "SELECT SUM(wc_postmeta1.meta_value) AS 'Total'
  5. ,wc_postmeta2.meta_value AS 'BillingEmail'
  6. ,CONCAT(wc_postmeta3.meta_value, ' ',wc_postmeta5.meta_value) AS BillingName
  7. ,Count(wc_postmeta2.meta_value) AS 'OrderCount' FROM ";
  8. $sql .= "{$wpdb->prefix}posts as wc_posts
  9. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta1 ON wc_postmeta1.post_id=wc_posts.ID
  10. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta2 ON wc_postmeta2.post_id=wc_posts.ID
  11. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta3 ON wc_postmeta3.post_id=wc_posts.ID
  12. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta5 ON wc_postmeta5.post_id=wc_posts.ID";
  13. $sql .= " WHERE wc_posts.post_type='shop_order'
  14. AND wc_postmeta1.meta_key='_order_total'
  15. AND wc_postmeta2.meta_key='_billing_email'
  16. AND wc_postmeta3.meta_key='_billing_first_name'
  17. AND wc_postmeta5.meta_key='_billing_last_name'";
  18. $sql .= " AND wc_posts.post_status IN ('wc-completed','wc-processing')";
  19. $sql .= " AND MONTH(wc_posts.post_date) = MONTH(CURRENT_DATE())AND YEAR(wc_posts.post_date) = YEAR(CURRENT_DATE())";
  20. $sql .= " GROUP BY wc_postmeta2.meta_value";
  21. $sql .= " Order By Total DESC";
  22. $sql .= " LIMIT 0,10";
  23. $result = $wpdb->get_results($sql, OBJECT);
  24.  
  25. echo"<table>";
  26. echo "<tr>";
  27. echo "<th>No</th>";
  28. echo "<th>Nama</th>";
  29. echo "<th>Order</th>";
  30. echo "<th>Belian (RM)</th>";
  31. echo "</tr>";
  32. $i=1;
  33. foreach ($result as $customer) {
  34. echo "<tr>";
  35. echo "<td>$i</td>";
  36. echo "<td>$customer->BillingName</td>";
  37. echo "<td>$customer->OrderCount</td>";
  38. echo "<td>$customer->Total</td>";
  39. echo "</tr>";
  40. $i++;
  41. }
  42. echo"</table>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement