1
我接到訂單。每個訂單都有一個類型,即'Vanilla'和一個尺寸,即'帶有數量的Mini。如何比較循環中的值?
如果有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;}
在'$ prevType'之後插入'$ count'並將其賦值爲'0'。連接使用點而不是加號。 '$ count = 0' | '$ count。= $ order_item ['qty']' – kosmos
爲什麼不使用帶有表示產品(「mini_vanilla」)的唯一鍵和表示數量的值的關聯數組?在每次迭代中,將數量添加到數組中,並在退出循環後顯示數組。 – Bogdan