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

cotxawn7  于 5个月前  发布在  Mysql
关注(0)|答案(1)|浏览(42)

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

add_action('manage_shop_order_posts_custom_column', 'display_order_count_column');
function display_order_count_column($column) {
    global $post;

    if ($column === 'order_count') {
        $customer_phone = get_post_meta($post->ID, '_billing_phone', true);

        // Премахване на разстоянията от телефонния номер
        $customer_phone = str_replace(' ', '', $customer_phone);

        if (!empty($customer_phone)) {
            $order_count = count(get_posts(array(
                'post_type'      => 'shop_order',
                'post_status'    => array('wc-completed', 'wc-order-rdy'),
                'meta_key'       => '_billing_phone',
                'meta_value'     => $customer_phone,
                'posts_per_page' => -1,
            )));

            if ($order_count >= 2) {
                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>';
            } else {
                echo '';
            }
        } else {
            echo '';
        }
    }

    if ($column === 'order_count') {
        $customer_phone = get_post_meta($post->ID, '_billing_phone', true);

        if (!empty($customer_phone)) {
            $order_count = count(get_posts(array(
                'post_type'      => 'shop_order',
                'post_status'    => array('wc-on-hold', 'wc-processing', 'wc-order-rdy'),
                'meta_key'       => '_billing_phone',
                'meta_value'     => $customer_phone,
                'posts_per_page' => -1,
            )));

            if ($order_count >= 2) {
                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>';
            } else {
                echo '';
            }
        } else {
            echo '';
        }
    }

    if ($column === 'order_count') {
        $customer_phone = get_post_meta($post->ID, '_billing_phone', true);

        if (!empty($customer_phone)) {
            $completed_processing_count = count(get_posts(array(
                'post_type'      => 'shop_order',
                'post_status'    => array('wc-completed', 'wc-processing', 'wc-order-rdy'),
                'meta_key'       => '_billing_phone',
                'meta_value'     => $customer_phone,
                'posts_per_page' => -1,
            )));

            $cancelled_failed_count = count(get_posts(array(
                'post_type'      => 'shop_order',
                'post_status'    => 'wc-cancelleddonttake',
                'meta_key'       => '_billing_phone',
                'meta_value'     => $customer_phone,
                'posts_per_page' => -1,
            )));


            if ($cancelled_failed_count > $completed_processing_count) {
                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>';
            } else {
                echo '';
            }
        } else {
            echo '';
        }
    }

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

7lrncoxx

7lrncoxx1#

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

// Utility function: get customer orders count grouped by order status (Lightweight SQL query)
function get_customer_orders_count_grouped_by_status( $billing_phone ) {
    global $wpdb; 
    return $wpdb->get_results( $wpdb->prepare( "
        SELECT p.post_status as status, COUNT(p.ID) as count 
        FROM {$wpdb->prefix}posts p
        LEFT JOIN {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id 
        WHERE p.post_type  = 'shop_order'
        AND pm.meta_key = '_billing_phone'
        AND pm.meta_value LIKE '%s'
        GROUP BY p.post_status
    ", $billing_phone ) );
}

add_action('manage_shop_order_posts_custom_column', 'display_customer_orders_count_column', 10 , 2);
function display_customer_orders_count_column( $column, $order_id ) {
    if ($column === 'order_count') {
        global $the_order;

        $billing_phone = $the_order->get_billing_phone();
        if ( ! empty($billing_phone) ) {
            $orders_count = get_customer_orders_count_grouped_by_status( $billing_phone );
            $count_paid   = $count_in_process = $count_cancelled = 0; // Initialize variables

            // Loop through customer orders count grouped by status
            foreach ( $orders_count as $order_data ) {
                if( in_array($order_data->status, ['wc-completed', 'wc-order-rdy'])) {
                    $count_paid += $order_data->count;
                }
                if( in_array($order_data->status, ['wc-on-hold', 'wc-processing', 'wc-order-rdy'])) {
                    $count_in_process += $order_data->count;
                }
                if( $order_data->status == 'wc-cancelleddonttake' ) {
                    $count_cancelled += $order_data->count;
                }
            }

            if ($count_paid >= 2) {
                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>';
            }

            if ($count_in_process >= 2) {
                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>';
            }

            if ($count_cancelled > $count_in_process) {
                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>';
            }
        }
    }
}

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

add_filter( 'manage_edit-shop_order_columns', 'add_customer_orders_count_column' );
function add_customer_orders_count_column( $columns ) {
    $new_columns = array();

    foreach ( $columns as $column_key => $column_label ) {
        if ( 'order_total' === $column_key ) {
            $new_columns['order_count'] = __('Count', 'woocommerce');
        }

        $new_columns[$column_key] = $column_label;
    }
    return $new_columns;
}

相关问题