I’m trying to write the product name and quantity from a given order, then read from the next order and add to the array but the final array only stores the value from the last loope
I don’t know what I’m doing wrong, I also tried array_push
I would like to count the number of units sold and display as follows for all orders:
Product A – 50 pieces
Product B – 30 pieces, etc.
foreach ($order_id as $order_ids) {
//echo '</br>' . $vendor_id . ' ' . $vendor_mail . ' ' . $vendor_name . '</br>';
$orderID = $order_ids->order_id;
$order_date = $order_ids->date_created;
$single_order = wc_get_order($orderID);
echo '</br>';
echo __('ID zamówienia: ') . $orderID . '<br>';
echo __('Data zamówienia: ') . $order_date . '<br>';
$product_quantity = array();
$product_name = array();
$single_order_array=array();
$array_merge=array();
foreach ($single_order->get_items() as $item) {
echo __('Nazwa produktu: ') . $item->get_name() . '<br>';
echo __('Ilość: ') . $item->get_quantity() . '<br><br><br>';
$product_quantity[] = $item->get_quantity();
$product_name[] = $item->get_name();
$single_order_array=array_combine($product_name, $product_quantity);
}
echo '</br>TABLICA POJEDYNCZEGO ZAMOWIENIA: </br>';
print_r($single_order_array);
echo '</br></br>';
$array_merge=array_replace_recursive($single_order_array, $array_merge);
}
echo '</br>TABLICA CAŁEGO WENDORA : </br>';
print_r($array_merge);
echo '</br></br>';
}
2
Answers
Original answer
According to your code
$single_order_array
will be overwritten within each loop offoreach ($single_order->get_items() as $item)
. So in this array you will get data only for last item from order.As for usage of
array_replace_recursive
I can’t quite get your idea. BTW how do you get you initial array$order_id
?EDIT
Try this code:
Sould work but not tested as I have no idea how orders’ data is stored into your input array
$order_id
.Your for loop is updating the values. You need to use associative array and store values in it. This will solve your problem.