我有订单进来。每个订单都有一个类型,即香草'和一个尺寸,即具有数量的迷你。
如果有5个迷你香草,我想让它显示一次不是5次。我也想计算数量。
这是我尝试过的很多事情之一
$prevSize = null;
$prevType= null;
foreach ($orders as $order){
echo '<tr>';
$new_order = new WC_Order();
$order_items = $new_order->get_items();
foreach ($order_items as $order_item ){
$currentSize = $order_item['pa_size'];
$currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
$currentType = $currentType[0]->name;
if($prevSize != $currentSize){
echo $currentType . '<br>';
echo $currentSize . '<br>';
}
$count += $order_item['qty'];
echo $count;
}
$prevSize = $currentSize;
$prevType = $currentType;}
答案 0 :(得分:0)
我们已经分析了您的查询,并根据您的需要,这是我的解决方案:
$order_type = array();
foreach ($orders as $order){
echo '<tr>';
$new_order = new WC_Order();
$order_items = $new_order->get_items();
$count = 0;
foreach ($order_items as $order_item ){
$currentSize = $order_item['pa_size'];
$currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
$currentType = $currentType[0]->name;
$temp_order_type = $currentSize.'_'.$currentType;
if(!in_array($temp_order_type, $order_type)){
$order_type[] = $temp_order_type;
echo $currentType . '<br>';
echo $currentSize . '<br>';
}
$count += $order_item['qty'];
}
echo $count;
}
以上代码将显示每次订单类型一次不显示多次。并显示每个订单中订购的数量。