0

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 2

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.

Sign up to request clarification or add additional context in comments.

13 Comments

That is why my question is how to store the data from the previous loop and add it to the new one each time. orderid I have a sql query which I did not paste here I would like to save the product name and quantity from all orders I use the yith vendor plugin and I make a summary report for each seller
So the answer is as suggested below - use $single_order_array[]=array_combine($product_name, $product_quantity); instead of $single_order_array=array_combine($product_name, $product_quantity); As for $array_merge=array_replace_recursive($single_order_array, $array_merge); I still don't get what's the idea here
You'd better provide an example array, otherwise it's too difficult to judge without knowing array structure.
@ncti, I tried to fix your code, check the result in the answer
@ncti, what's the purpose of asking questions here and not bothering to check the answer?
|
0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.