WooCommerce 从所有订单中获取项目元数据 [英] WooCommerce Get Item Meta from All Orders

查看:32
本文介绍了WooCommerce 从所有订单中获取项目元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示来自 WooCommerce 插件的所有已完成订单的所有订单项目(带有项目元).我还想将显示限制为仅 10 个订单项.我已经弄清楚如何显示所有订单项目,但不能将数量限制为 10.这是我目前用于显示所有订单项目的代码:

$args = 数组('post_type' =>'shop_order','post_status' =>'发布','posts_per_page' =>-1,'tax_query' =>大批(大批('分类' =>'shop_order_status','字段' =>'蛞蝓','条款' =>数组('完成'))));$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['预订日期'];$time = $item['预订时间'];$fname = $item['名字 - 名字'];$church = $item['教会信息 - 教会名称'];$city = $item['教堂信息 - 城市'];$state = $item['教会信息 - 州'];?><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 class="wc-upcoming-details"><?php echo $fname .', ' .$教会.', ' .$城市.', ' .$状态;?>

<?php }终了;

此代码查询所有已完成的订单,然后为每个查询的订单循环遍历所有订单项.有些订单可能有 1 个以上的订单项目,因此如果我将每页的帖子数限制为 10,并且所有这些订单都有 5 个订单项目,那么我将总共显示 50 个订单项目.我尝试在 foreach 循环中添加一个迭代"变量来限制它,但这只循环遍历一个订单的5"个订单项,而不是整个50"个订单项.

首先,我需要通过 $date 和 $time 变量对所有订单项进行排序(我相信我可以通过使用 strtotime() 函数将它们转换为时间戳来实现).

其次,我只想显示所有订单中的前 10 个订单项目(基于时间戳).

任何想法如何修改此代码以允许这样做?

解决方案

我认为 wordpress 不会为您提供此类查询.你必须建立自己的逻辑来做到这一点.希望这可以帮助你...

$args = 数组('post_type' =>'shop_order','post_status' =>'发布','posts_per_page' =>-1,'tax_query' =>大批(大批('分类' =>'shop_order_status','字段' =>'蛞蝓','条款' =>数组('完成'))));$count = 0;$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 ) {如果($计数> 10){打破2;//中断两个循环}$count++;$date = $item['预订日期'];$time = $item['预订时间'];$fname = $item['名字 - 名字'];$church = $item['教会信息 - 教会名称'];$city = $item['教堂信息 - 城市'];$state = $item['教会信息 - 州'];?><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 class="wc-upcoming-details"><?php echo $fname .', ' .$教会.', ' .$城市.', ' .$状态;?>

<?php }终了;

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?

解决方案

I don't think wordpress provide you such type of query. You have to build your own logic to do this. Hope this may help you...

$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')
            )
          )
   );

         $count = 0;
         $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 ) {   

                    if( $count > 10){
                       break 2; //break both loops 
                    } 
                    $count++;
                    $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;

这篇关于WooCommerce 从所有订单中获取项目元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆