我正在尝试为WooCommerce插件中的所有已完成订单显示所有订单商品(包含Item Meta)。我还想将显示限制为仅10个订单商品。我已经想出如何显示所有订单商品但不能将数量限制为10.以下是我目前用于显示所有订单商品的代码:
$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;
此代码查询所有已完成的订单,然后循环查看每个查询订单的所有订单项。某些订单可能包含多个订单商品,因此如果我将每页订单限制为10并且所有订单都有5个订单商品,那么我将显示总共50个订单商品。我尝试在foreach循环中添加一个“iterate”变量来限制它,但是只循环一个Order的“5”订单项而不是总共“50”订单项。
首先,我需要通过$ date和$ time变量对所有订单项进行排序(我相信我可以通过使用strtotime()函数将它们转换为时间戳来实现)。
其次,我想在所有订单中只显示前10个订单项(基于时间戳)。
任何想法如何修改此代码以实现此目的?
答案 0 :(得分:0)
这样的事情应该将显示的项目数限制为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;?>
如果我理解你正确寻找的东西应该可以解决问题。
答案 1 :(得分:0)
我不认为wordpress会为您提供此类查询。你必须建立自己的逻辑来做到这一点。希望这可以帮助你...
$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;