Reputation: 31
I'm still very new to PHP and WordPress and I'm struggling to properly json_encode an array that has multiple woocommerce order items in it. The "order items" part of the array is the part I can't figure out because the structure needs to be dynamically created based on the number of SKUs in the order (i.e 1,2,3, etc).
An example of the dynamic part of the array I need to create is below.
$product_list_example_array =
array (
'items' => array (
0 =>
array (
'quantity' => 1,
'sku' => 'sample string 2',
'description' => 'sample string 3',
'price' => 4,
),
1 =>
array (
'quantity' => 1,
'sku' => 'sample string 2',
'description' => 'sample string 3',
'price' => 4,
),
),
);
To do this I created the variable $product_list which gives me my array perfectly formatted, based on the amount of products. When I go to echo out the $product_list it looks exactly like the sample above, just all on one line.
So I thought would work great... however, when I go to put the $product_list inside the bigger array, and then use json_encode to convert that array into json, low an behold, my $product_list variable just doesn't convert. The $product_list that appears in the final json is still in array format!
foreach( $order_item as $product ) {
$product_name = $product['name'];
$product_qty = $product['qty'];
$product_id = $product['product_id'];
$product_price = $product['line_total'];
$product_sku = get_post_meta( $product_id, '_sku', true);
$prodct_list_name[] = '['. $items_number++ .']=> array (\'quantity\' => '. $product_qty .',\'sku\' => '. $product_sku .',\'description\' => '. $product_name .'\'price\' => '. $product_price .')';
}
$product_list = implode( ',', $prodct_list_name );
echo $product_list;
$args = array (
'apiKey' => $API_KEY,
'booking' =>
array (
//this is the part of the array where I insert my $product_list array object
'items' =>
array ( $product_list
),
'pickupDetail' =>
array (
'address' => $Pickup_Address,
),
'dropoffDetail' =>
array (
'name' => $Shipping_Name,
'phone' => $Phone_Number,
'email' => $Shipping_Email,
'description' => $Order_Notes,
'address' => $Shipping_Address,
),
),
);
//display the json
$json_data = json_encode($args);
echo $json_data;
And finally the json appears as below. Please take a look at the json and see how the entire array got encoded correctly except the $product_list part. I'd really love if there is a way to get around this or go about it entirely differently.
{"apiKey":API_KEY,"booking":{"items":["[0]=> array ('quantity' => 1,'sku' => CAN-BR,'description' => Candle | Black Raspberry'price' => 0),[1]=> array ('quantity' => 1,'sku' => ,'description' => Candle | Fig & Melon'price' => 21.95),"],"pickupDetail":{"address":"Pickup Address"},"dropoffDetail":{"name":"Cusomter Name","phone":"5555555555","email":"[email protected]","description":"Order Notes (order_comments)","address":"Delivery Address"}}}
Upvotes: 1
Views: 1536
Reputation: 3354
Because you use it as string and not an array. Use following code to store $product_list
as an array:
$product_list = array();
foreach( $order_item as $product ) {
$product_name = $product['name'];
$product_qty = $product['qty'];
$product_id = $product['product_id'];
$product_price = $product['line_total'];
$product_sku = get_post_meta( $product_id, '_sku', true);
$product_list[$items_number++] = array ('quantity' => $product_qty,'sku' => $product_sku ,'description' => $product_name, 'price' => $product_price );
}
// $product_list = implode( ',', $prodct_list_name );
// echo $product_list;
$args = array (
'apiKey' => $API_KEY,
'booking' =>
array (
//this is the part of the array where I insert my $product_list array object
'items' =>
array ( $product_list
),
'pickupDetail' =>
array (
'address' => $Pickup_Address,
),
'dropoffDetail' =>
array (
'name' => $Shipping_Name,
'phone' => $Phone_Number,
'email' => $Shipping_Email,
'description' => $Order_Notes,
'address' => $Shipping_Address,
),
),
);
//display the json
$json_data = json_encode($args);
echo $json_data;
Upvotes: 1