0

The below code is not rendering the line_items as a JSON array and I have searched/asked around to no avail.

Can anyone give me a hand here?

I've also included the json_encode data of the $orderShippingInfo variable.

Below is the JSON output:

{
"token":"API_KEY_HERE",
"email":"[email protected]",
"line_items":{
   "sku":"12345",
   "name":"Product Name",
   "title":"Product Title",
   "price":"$1.99",
   "quantity":"1",
   "total_tax":"$0.00"
   },
"shipping_lines":{
   "title":"UPS",
   "price":"$0.00",
   "method":"UPS 3 DAY",
   "carrier":"UPS"
   },
"order_id":"0001",
"profile":"default",
"shipping_address":{
   "province":"AZ",
   "city":"Testville",
   "first_name":"Augusto",
   "last_name":"Pinochet",
   "zip":"12341",
   "province_code":"NY",
   "country":"US",
   "company":"Company Name, Inc.",
   "phone":"1112223333",
   "country_code":"US",
   "address1":"123 Testing Dr Street",
   "address2":"Suite #1"
   },
"subtotal_price":"$0.00",
"created_at":"2017-02-02",
"country_code":"US",
"total_discounts":"$0.00",
"total_price":"$0.00"
}

TIA!

<?php

$orderShippingInfo = array(
    'token' => 'API_KEY_HERE',
    'email' => '[email protected]',
    'line_items' => array(
        'sku'=>'12345',
        'name'=>'Product Name',
        'title'=>'Product Title',
        'price'=> '$1.99',
        'quantity' => '1',
        'total_tax' => '$0.00',
    ),
    'shipping_lines' => array(
        'title' => 'UPS',
        'price' => '$0.00',
        'method' => 'UPS 3 DAY',
        'carrier' => 'UPS',
    ),
    'order_id' => '0001',
    'profile' => 'default',
    'shipping_address' => array(
        'province' => 'AZ',
        'city' => 'Testville',
        'first_name' => 'Augusto',
        'last_name' => 'Pinochet',
        'zip' => '12341',
        'province_code' => 'NY',
        'country' => 'US',
        'company' => 'Company Name, Inc.',
        'phone' => '1112223333',
        'country_code' => 'US',
        'address1' => '123 Testing Dr Street',
        'address2' => 'Suite #1',
    ),
    'subtotal_price' => '$0.00',
    'created_at' => date('Y-m-d'),
    'country_code' => 'US',
    'total_discounts' => '$0.00',
    'total_price' => '$0.00',
);

echo json_encode($orderShippingInfo);

?>
3
  • $orderShippingInfo = json_decode($json, true); Commented Feb 2, 2017 at 22:13
  • @Mohammad He is ENCODING a PHP Array Commented Feb 2, 2017 at 22:15
  • so you want the line_items to remain an array rather than a json string? Commented Feb 2, 2017 at 22:35

2 Answers 2

2

You need to make your array of line items an array of them.

For example:

$orderShippingInfo = array(
'token' => 'API_KEY_HERE',
'email' => '[email protected]',
'line_items' => array(
    array(
        'sku'=>'12345',
        'name'=>'Product Name',
        'title'=>'Product Title',
        'price'=> '$1.99',
        'quantity' => '1',
        'total_tax' => '$0.00',
    )
),
'shipping_lines' => array(
    'title' => 'UPS',
    'price' => '$0.00',
    'method' => 'UPS 3 DAY',
    'carrier' => 'UPS',
),
'order_id' => '0001',
'profile' => 'default',
'shipping_address' => array(
    'province' => 'AZ',
    'city' => 'Testville',
    'first_name' => 'Augusto',
    'last_name' => 'Pinochet',
    'zip' => '12341',
    'province_code' => 'NY',
    'country' => 'US',
    'company' => 'Company Name, Inc.',
    'phone' => '1112223333',
    'country_code' => 'US',
    'address1' => '123 Testing Dr Street',
    'address2' => 'Suite #1',
),
'subtotal_price' => '$0.00',
'created_at' => date('Y-m-d'),
'country_code' => 'US',
'total_discounts' => '$0.00',
'total_price' => '$0.00',
);

Then you can add a second line item to the array if you require.

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

1 Comment

The same may be true for your shipping_lines also
1

The reason for this is that javascript does not have an associative array concept, so to produce one out of a json_encode() would break javascript.

See this example of what json_encode will do

$xx = ['a','b'];

$yy = ['one'=> 1, 'two'=>2];

print_r($xx);
echo json_encode($xx).PHP_EOL;
print_r($yy);
echo json_encode($yy);


Array
(
    [0] => a
    [1] => b
)
["a","b"]    // note this is a JSON array
Array
(
    [one] => 1
    [two] => 2
)
{"one":1,"two":2}  // note this is a JSON object

If the PHP array is numerically indexed, you get a JSON array

If the PHP array is assoc array it will create an JSON Object

If for some reason you specifically want a JSON String representation to be an array you could just add a [] to the $orderShippingInfo[] = array( like this

$orderShippingInfo[] = array(
'token' => 'API_KEY_HERE',
'email' => '[email protected]',
'line_items' => array(
    'sku'=>'12345',
    'name'=>'Product Name',
    'title'=>'Product Title',
    'price'=> '$1.99',
. . .
. . .

2 Comments

This is correct. JavaScript doesn't have associative arrays so it must convert the data into an object.
Thanks for the clarification, I had forgotten the reason.

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.