显示旧订单数量的代码有问题

问题描述 投票:0回答:1

我正在使用创建一列的代码来显示用户是否有具有某些状态的旧订单。

问题是,有时当我刷新订单页面时,会发生一些事情,它会给我 10/15/30 个缓慢的请求,如下所示:https://prnt.sc/nnjGy4rvgzkY

我使用的代码是这个。有谁知道问题出在哪里?

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 '';
        }
    }

我不知道问题出在哪里。

php mysql wordpress woocommerce orders
1个回答
0
投票

在显示管理订单列表页面时,您的代码正在按行执行 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 文件中(或插件中)。已测试并有效。


对于其他读者,这是缺少添加“计数”列的功能:

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.