skip to Main Content

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


  1. Original answer

    According to your code $single_order_array will be overwritten within each loop of foreach ($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:

    $results=array(); // place this outside all loops
    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();
                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($product_name => $product_quantity);
                }
    
                echo '</br>TABLICA POJEDYNCZEGO ZAMOWIENIA: </br>';
                print_r($single_order_array);
                echo '</br></br>';
    
                foreach ($single_order_array as $product=>$quantity){
                    if (array_key_exists($product, $results)){
                    $results[$product]+=$quantity;}
                    else {$results += array ($product=>$quantity);
                    }
                }
            }
            echo '</br>TABLICA CAŁEGO WENDORA : </br>';
            print_r($results);
            echo '</br></br>';
    

    Sould work but not tested as I have no idea how orders’ data is stored into your input array $order_id.

    Login or Signup to reply.
  2. Your for loop is updating the values. You need to use associative array and store values in it. This will solve your problem.

    $data = [];
    
    for($i =0; $i<sizeof($items); $i++) 
    {
    
    $data[$i]['item_value_1'] = $items[$i]->item_value_1;
    $data[$i]['item_value_2'] = $items[$i]->item_value_2;
    $data[$i]['item_value_3'] = $items[$i]->item_value_3;
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search