【问题标题】:Get a custom field count for Woocommerce orders获取 Woocommerce 订单的自定义字段计数
【发布时间】:2023-03-22 19:05:01
【问题描述】:

您好,我正在开发 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
代理1 2
特工2 3
特工1 4

我的预期输出:

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

你们能帮我解决这个问题吗?我错过了什么?

【问题讨论】:

    标签: php sql wordpress woocommerce custom-fields


    【解决方案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 文件中。

    经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      相关资源
      最近更新 更多