fly2high

Roles Based Top Customer

Aug 13th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 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. JOIN {$wpdb->prefix}users as wc_users6
  10. JOIN {$wpdb->prefix}usermeta as wc_usermeta6 ON wc_usermeta6.user_id=wc_users6.ID
  11. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta1 ON wc_postmeta1.post_id=wc_posts.ID
  12. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta2 ON wc_postmeta2.post_id=wc_posts.ID
  13. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta3 ON wc_postmeta3.post_id=wc_posts.ID
  14. LEFT JOIN {$wpdb->prefix}postmeta as wc_postmeta5 ON wc_postmeta5.post_id=wc_posts.ID";
  15. $sql .= " WHERE wc_posts.post_type='shop_order'
  16. AND wc_postmeta1.meta_key='_order_total'
  17. AND wc_postmeta2.meta_key='_billing_email'
  18. AND wc_postmeta3.meta_key='_billing_first_name'
  19. AND wc_postmeta5.meta_key='_billing_last_name'
  20. AND wc_usermeta6.meta_key = 'wp_capabilities'
  21. AND wc_usermeta6.meta_value LIKE '%Goldmember%'";
  22.  
  23. $sql .= " AND wc_posts.post_status IN ('wc-completed','wc-processing')";
  24. $sql .= " AND MONTH(wc_posts.post_date) = MONTH(CURRENT_DATE())AND YEAR(wc_posts.post_date) = YEAR(CURRENT_DATE())";
  25. $sql .= " GROUP BY wc_postmeta2.meta_value";
  26. $sql .= " Order By Total DESC";
  27. $sql .= " LIMIT 0,10";
  28. $result = $wpdb->get_results($sql, OBJECT);
  29.  
  30. echo"<table>";
  31. echo "<tr>";
  32. echo "<th>No</th>";
  33. echo "<th>Nama</th>";
  34. echo "<th>Order</th>";
  35. echo "<th>Belian (RM)</th>";
  36. echo "</tr>";
  37. $i=1;
  38. foreach ($result as $customer) {
  39. echo "<tr>";
  40. echo "<td>$i</td>";
  41. echo "<td>$customer->BillingName</td>";
  42. echo "<td>$customer->OrderCount</td>";
  43. echo "<td>$customer->Total</td>";
  44. echo "</tr>";
  45. $i++;
  46. }
  47. echo"</table>";
Advertisement
Add Comment
Please, Sign In to add comment