0

I have a little code to create an order which don't work with php7

foreach ($object['products'] as $productInfoSrc) {
        $productId = $productInfoSrc['product_id'];
        $qty = $productInfoSrc['qty'];
        $data['order']['add_products'][$productId] = array('qty' => $qty);
}

The line $data['order']['add_products'][$productId] = array('qty' => $qty); return me the bug php.

I try to make some correction like this :

    foreach ($object['products'] as $productInfoSrc) {
        $productId = $productInfoSrc['product_id'];
        $qty = $productInfoSrc['qty'];
        $data['order']['add_products']= array($productId, array('qty' => $qty);
}

but if I have 2 product, just 1 was taken by this code (i don't merge array i think).

someone can help me ?

3
  • Can you post the error message? Also can you post the result of <code>var_dump($productInfoSrc);</code>? Probably <code>$productId</code> is an array instead of string.
    – Sebastian
    Oct 21, 2018 at 8:53
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – Piyush
    Oct 22, 2018 at 5:15
  • As far I understand he has a php error in $data['order']['add_products'][$productId] = array('qty' => $qty); which is array to string conversion. So $productId has to be an array - which I typed in my answer. If he changes it, the script should work. Why this it not an answer?
    – Sebastian
    Oct 22, 2018 at 5:27

1 Answer 1

0

As far as I have understood your problem, You are getting only one product information in variable $data['order']['add_product']

You can try below code

foreach ($object['products'] as $productInfoSrc) {
   $productId = $productInfoSrc['product_id'];
   $qty = $productInfoSrc['qty'];
   $data['order']['add_products'][]= array($productId, array('qty' => $qty);
}

I just added [] after $data['order']['add_products'].

Now you can dump this variable like below to check it data---

var_dump($data['order']['add_products'])

Please put comment if it works or you need something else.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.