douchu5131 2014-07-01 17:23
浏览 74

WooCommerce从所有订单获取项目元

I am trying to display all Order Items (with the Item Meta) for All completed orders from the WooCommerce plugin. I also want to limit the display to 10 order items only. I have figured out how to display ALL Order Items but cannot limit the number to 10. Here is the code I am currently using to display All Order Items:

$args = array(
                    'post_type' => 'shop_order',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'shop_order_status',
                            'field' => 'slug',
                            'terms' => array('completed')
                        )
                    )
                );

                $loop = new WP_Query( $args );

                while ( $loop->have_posts() ) : $loop->the_post();
                    $order_id = $loop->post->ID;
                    $order = new WC_Order($order_id);

                    foreach( $order->get_items() as $item ) {   

                        $date = $item['Booking Date'];
                        $time = $item['Booking Time'];
                        $fname = $item['First Name - First Name'];
                        $church = $item['Church Information - Church Name'];
                        $city = $item['Church Information - City'];
                        $state = $item['Church Information - State'];
                    ?>

                        <div class="wc-upcoming-booking">
                            <div class="wc-upcoming-time">
                                <span class="upcoming-hour"><?php echo $time; ?></span>
                                <span class="upcoming-date"><?php echo $date; ?></span>
                            </div>
                            <div class="wc-upcoming-details">
                                <?php echo $fname . ', ' . $church . ', ' . $city . ', ' . $state; ?>
                            </div>
                        </div>

                    <?php }

                endwhile;

This code queries ALL Completed Orders and then loops through ALL Order Items for each queried Order. Some Orders may have more than 1 Order Item so if I limit the Posts Per Page to 10 and all of those Orders have 5 Order Items than I will have a total of 50 Order Items displayed. I tried adding an "iterate" variable to the foreach loop to limit that but that only loops through the "5" Order Items for one Order and NOT the total "50" Order Items.

First, I need to ORDER all Order Items by the $date and $time variable (which I believe I can do by converting them to a Timestamp using the strtotime() function).

Second, I would like to display to ONLY the first 10 Order Items (based on the timestamp) from All Orders.

Any ideas how to modify this code to allow for this?

  • 写回答

2条回答 默认 最新

  • doujianglin6704 2015-04-10 05:40
    关注

    Something like this should limit the amount of items displayed to 10:

    <?php
    $args = array(
        'post_type' => 'shop_order',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'tax_query' => array(
            array(
                'taxonomy' => 'shop_order_status',
                'field' => 'slug',
                'terms' => array('completed')
            )
        )
    );
    $orders=get_posts($args);
    $i=0;
    foreach($orders as $o):
        if($i>10){
            break;
        }
        $order_id = $o->ID;
        $order = new WC_Order($order_id);
        foreach( $order->get_items() as $item ):
            if($i>10){
                break;
            }
            $i++;
            $date = $item['Booking Date'];
            $time = $item['Booking Time'];
            $fname = $item['First Name - First Name'];
            $church = $item['Church Information - Church Name'];
            $city = $item['Church Information - City'];
            $state = $item['Church Information - State'];
        ?>
            <div class="wc-upcoming-booking">
                <div class="wc-upcoming-time">
                    <span class="upcoming-hour"><?php echo $time; ?></span>
                    <span class="upcoming-date"><?php echo $date; ?></span>
                </div>
                <div class="wc-upcoming-details">
                    <?php echo $fname . ', ' . $church . ', ' . $city . ', ' . $state; ?>
                </div>
            </div>
        <?php endforeach;
    endforeach;?>
    

    If I understand what you're looking for correctly that should do the trick.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图