0

A newbie question.

I have this data coming in:

foreach ( $items as $item_id => $item_data ) {
  $item_data['name'];
  $item_data['qty'];
  $item_data['variation_id'];
  ...
}

So, it can be more than one item.

How to place this data into this:

     $order = $pf->post('orders',
    [

        'items' => [
            [
                'variant_id' => $item_data['variation_id'],// Small poster
                'name' => $item_data['name'], // Display name
                'retail_price' => $order_details['_order_total'][0], // Retail price for packing slip
                'quantity' => $item_data['qty'],
                'files' => [
                    [
                        'url' => 'http://example.com/files/posters/poster_1.jpg',
                    ],
                ],
            ],
        ],
    ]
);

I mean the looping part. As you can see right now it is kind of hardcoded and for one item only. How it should look like if there is more items?

1
  • What do you mea by "How to place this data"? Commented Feb 14, 2017 at 8:17

1 Answer 1

1

This should do the trick for you:

$mappedItems = [];
foreach ( $items as $item_id => $item_data ) {
  $mappedItems[] = [
     'variant_id' => $item_data['variation_id'],// Small poster
     'name' => $item_data['name'], // Display name
     'retail_price' => $order_details['_order_total'][0], // Retail price for packing slip
     'quantity' => $item_data['qty'],
     'files' => [
         [
           'url' => 'http://example.com/files/posters/poster_1.jpg',
         ],
      ],
  ];
}

$order = $pf->post('orders',
[
    'items' => $mappedItems
]
Sign up to request clarification or add additional context in comments.

1 Comment

OMG, awesome! Thanks a lot!

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.