mysql 在管理订单列表中显示按状态分组的客户订单计数

cotxawn7  于 2024-01-05  发布在  Mysql
关注(0)|答案(1)|浏览(148)

我使用的代码在WooCommerce管理订单列表中添加一列,显示来自特定订单状态的客户订单。
问题是,有时当我刷新订单页面时,会发生一些事情,它会给我10/15/30这样的缓慢请求:
x1c 0d1x的数据
我使用的代码是这样的。有人知道问题可能来自哪里吗?

  1. add_action('manage_shop_order_posts_custom_column', 'display_order_count_column');
  2. function display_order_count_column($column) {
  3. global $post;
  4. if ($column === 'order_count') {
  5. $customer_phone = get_post_meta($post->ID, '_billing_phone', true);
  6. // Премахване на разстоянията от телефонния номер
  7. $customer_phone = str_replace(' ', '', $customer_phone);
  8. if (!empty($customer_phone)) {
  9. $order_count = count(get_posts(array(
  10. 'post_type' => 'shop_order',
  11. 'post_status' => array('wc-completed', 'wc-order-rdy'),
  12. 'meta_key' => '_billing_phone',
  13. 'meta_value' => $customer_phone,
  14. 'posts_per_page' => -1,
  15. )));
  16. if ($order_count >= 2) {
  17. echo '<a href="' . admin_url("edit.php?s=" . $customer_phone . "&post_status=all&post_type=shop_order&action=-1&m=0&_customer_user&paged=1&action2=-1") . '" class="Button_all_status" title="Числото показва броя на успешно завършените поръчки, които клиента има.">' . $order_count . '</a>';
  18. } else {
  19. echo '';
  20. }
  21. } else {
  22. echo '';
  23. }
  24. }
  25. if ($column === 'order_count') {
  26. $customer_phone = get_post_meta($post->ID, '_billing_phone', true);
  27. if (!empty($customer_phone)) {
  28. $order_count = count(get_posts(array(
  29. 'post_type' => 'shop_order',
  30. 'post_status' => array('wc-on-hold', 'wc-processing', 'wc-order-rdy'),
  31. 'meta_key' => '_billing_phone',
  32. 'meta_value' => $customer_phone,
  33. 'posts_per_page' => -1,
  34. )));
  35. if ($order_count >= 2) {
  36. echo '<a href="' . admin_url("edit.php?s=" . $customer_phone . "&post_status=all&post_type=shop_order&action=-1&m=0&_customer_user&paged=1&action2=-1") . '" class="Button_in_process" title="Числото показва броя на поръчките, които клиента има в обработка.">' . $order_count . '</a>';
  37. } else {
  38. echo '';
  39. }
  40. } else {
  41. echo '';
  42. }
  43. }
  44. if ($column === 'order_count') {
  45. $customer_phone = get_post_meta($post->ID, '_billing_phone', true);
  46. if (!empty($customer_phone)) {
  47. $completed_processing_count = count(get_posts(array(
  48. 'post_type' => 'shop_order',
  49. 'post_status' => array('wc-completed', 'wc-processing', 'wc-order-rdy'),
  50. 'meta_key' => '_billing_phone',
  51. 'meta_value' => $customer_phone,
  52. 'posts_per_page' => -1,
  53. )));
  54. $cancelled_failed_count = count(get_posts(array(
  55. 'post_type' => 'shop_order',
  56. 'post_status' => 'wc-cancelleddonttake',
  57. 'meta_key' => '_billing_phone',
  58. 'meta_value' => $customer_phone,
  59. 'posts_per_page' => -1,
  60. )));
  61. if ($cancelled_failed_count > $completed_processing_count) {
  62. echo '<a href="' . admin_url("edit.php?s=" . $customer_phone . "&post_status=all&post_type=shop_order&action=-1&m=0&_customer_user&paged=1&action2=-1") . '" class="Button_bad_client" title="Рисков клиент. Моля, проверете поръчките на клиента.Клиента има повече провалени от колкото изпълнени поръчки">' . $cancelled_failed_count . '</a>';
  63. } else {
  64. echo '';
  65. }
  66. } else {
  67. echo '';
  68. }
  69. }

字符串
我不知道会有什么问题。

7lrncoxx

7lrncoxx1#

你的代码是执行4重职位查询行时显示管理订单列表页面,所以它是正常的,你得到缓慢的请求...
相反,您可以使用一个独特的轻量级自定义SQL查询(按行),它将按订单状态为您提供给予客户订单计数组。
请尝试以下操作:

  1. // Utility function: get customer orders count grouped by order status (Lightweight SQL query)
  2. function get_customer_orders_count_grouped_by_status( $billing_phone ) {
  3. global $wpdb;
  4. return $wpdb->get_results( $wpdb->prepare( "
  5. SELECT p.post_status as status, COUNT(p.ID) as count
  6. FROM {$wpdb->prefix}posts p
  7. LEFT JOIN {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id
  8. WHERE p.post_type = 'shop_order'
  9. AND pm.meta_key = '_billing_phone'
  10. AND pm.meta_value LIKE '%s'
  11. GROUP BY p.post_status
  12. ", $billing_phone ) );
  13. }
  14. add_action('manage_shop_order_posts_custom_column', 'display_customer_orders_count_column', 10 , 2);
  15. function display_customer_orders_count_column( $column, $order_id ) {
  16. if ($column === 'order_count') {
  17. global $the_order;
  18. $billing_phone = $the_order->get_billing_phone();
  19. if ( ! empty($billing_phone) ) {
  20. $orders_count = get_customer_orders_count_grouped_by_status( $billing_phone );
  21. $count_paid = $count_in_process = $count_cancelled = 0; // Initialize variables
  22. // Loop through customer orders count grouped by status
  23. foreach ( $orders_count as $order_data ) {
  24. if( in_array($order_data->status, ['wc-completed', 'wc-order-rdy'])) {
  25. $count_paid += $order_data->count;
  26. }
  27. if( in_array($order_data->status, ['wc-on-hold', 'wc-processing', 'wc-order-rdy'])) {
  28. $count_in_process += $order_data->count;
  29. }
  30. if( $order_data->status == 'wc-cancelleddonttake' ) {
  31. $count_cancelled += $order_data->count;
  32. }
  33. }
  34. if ($count_paid >= 2) {
  35. echo '<a href="' . admin_url("edit.php?s=" . $billing_phone . "&post_status=all&post_type=shop_order&action=-1&m=0&_customer_user&paged=1&action2=-1") . '" class="Button_all_status" title="Числото показва броя на успешно завършените поръчки, които клиента има.">' . $count_paid . '</a>';
  36. }
  37. if ($count_in_process >= 2) {
  38. echo '<a href="' . admin_url("edit.php?s=" . $billing_phone . "&post_status=all&post_type=shop_order&action=-1&m=0&_customer_user&paged=1&action2=-1") . '" class="Button_in_process" title="Числото показва броя на поръчките, които клиента има в обработка.">' . $count_in_process . '</a>';
  39. }
  40. if ($count_cancelled > $count_in_process) {
  41. echo '<a href="' . admin_url("edit.php?s=" . $billing_phone . "&post_status=all&post_type=shop_order&action=-1&m=0&_customer_user&paged=1&action2=-1") . '" class="Button_bad_client" title="Рисков клиент. Моля, проверете поръчките на клиента.Клиента има повече провалени от колкото изпълнени поръчки">' . $count_cancelled . '</a>';
  42. }
  43. }
  44. }
  45. }

字符串
代码放在你的子主题的functions.php文件中(或插件中)。测试和工作。
对于其他读者,这里是缺少的功能,添加一个“计数”列到管理员WooCommerce订单列表:

  1. add_filter( 'manage_edit-shop_order_columns', 'add_customer_orders_count_column' );
  2. function add_customer_orders_count_column( $columns ) {
  3. $new_columns = array();
  4. foreach ( $columns as $column_key => $column_label ) {
  5. if ( 'order_total' === $column_key ) {
  6. $new_columns['order_count'] = __('Count', 'woocommerce');
  7. }
  8. $new_columns[$column_key] = $column_label;
  9. }
  10. return $new_columns;
  11. }

展开查看全部

相关问题