获取 Woocommerce 订单的自定义字段计数

Get a custom field count for Woocommerce orders

你好,我正在做 woocommerce

我在 Woocommerce 中为订单添加了自定义字段,这是一个代理名称,每个订单都分配有一个代理。

我想获取代理名称及其总订单数。

我已经创建了自定义小部件并且它运行良好,只是每次都会出现代理名称问题。我只需要一次代理名称及其总数。

下面是我试过的代码:

function wc_orders_dashboard_widget_function() {
    $args   = array(
            'meta_key'          =>'_wc_acof_2',
            'post_type'         => 'shop_order',
            'post_status'       => 'All',
            'posts_per_page'    => 5 
          );
    $orders = get_posts( $args );

    if( count( $orders ) > 0 ) {
        ?>      
        <table width="100%" class="vao_orders_table">
            <tr>
                <th><?php _e( 'Agent Name', 'woocommerce' ); ?></th>
                <th><?php _e( 'Order Count', 'woocommerce' ); ?></th>
            </tr>
        <?php       
        foreach ( $orders as $key => $value ) {

            ?>
            <tr>
                <td>
                <?php

                $order   =   new WC_Order( $value->ID );

                $order_id = $order->get_order_number();

                $agent_name = get_post_meta( $order_id, '_wc_acof_2', true );

                // 1. Get the order ID and its link
                if ( $agent_name ) {                
                    echo $agent_name;
                ?>
                </td>               
                <td>                

                <?php
                    // 2. Get status of the order
                    $order_count += wp_count_posts($value->post_type );
                    echo $order_count;
                }
                ?>
                </td>               

            </tr>
            <?php           
        }

        ?></table><?php

        // 4. Adding All orders link which will redirect to Orders page of WooCommerce
        printf( '<div id="vao_orders"><span class="dashicons dashicons-cart"></span> <a href="%s">' . __( 'View all orders' ) . '</a></div>', admin_url( 'edit.php?post_type=shop_order' ) );       
    }else{
        // If no orders then display No Orders message
        echo __( 'No Orders.' );
    }
}

但它显示如下输出:

代理名称订单计数
代理人 1
特工 1 2
特工 2 3
特工 1 4

我的预期输出:

代理名称订单计数
特工 1 3
特工 2 1

你们能帮我解决这个问题吗?我缺少什么?

这可以通过非常简单的 SQL 查询来完成,这将压缩您的函数:

function wc_orders_dashboard_widget_function() {
    global $wpdb;
    $domain = 'woocommerce';

    // The SQL query
    $results = $wpdb->get_results( "
        SELECT meta_value as value, count(meta_value) as count
        FROM {$wpdb->prefix}postmeta
        WHERE meta_key LIKE '_wc_acof_2'
        GROUP BY meta_value
        ORDER BY meta_value
    " );

    if( count( $results ) > 0 ):
        ?>      
        <table width="100%" class="vao_orders_table">
            <tr>
                <th><?php _e( 'Agent Name', $domain ); ?></th>
                <th><?php _e( 'Order Count', $domain ); ?></th>
            </tr>
        <?php foreach ( $results as $result ): ?>
            <tr>
                <td><?php echo $result->value; ?></td>               
                <td><?php echo $result->count; ?></td>
            </tr>
        <?php endforeach; ?>
        </table>
        <?php

        // 4. Adding All orders link which will redirect to Orders page of WooCommerce
        printf( '<div id="vao_orders">
            <span class="dashicons dashicons-cart"></span> <a href="%s">%s</a>
        </div>', admin_url( 'edit.php?post_type=shop_order' ), __( 'View all orders', $domain ) );       
    else:
        // If no orders then display No Orders message
        echo __( 'No Orders.', $domain );
    endif;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。

已测试并有效。