I’m trying to follow http://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-order-notes to get order notes from my orders and get the author and id inside a nested json respons but it’s not working
Does anything stand out as being wrong here?
require_once 'auth.php';
$notes = $woocommerce->get('orders/108668/notes');
$json = json_decode($notes, true);
foreach($json as $elem) {
echo($elem['id']. ", ".$elem['author'] );
echo("<br/>");
}
If i print_r($notes);
I get the response like so
Array ( [0] => stdClass Object ( [id] => 24721 [author] => haspden [date_created] => 2020-12-07T09:03:01 [date_c........
if i print_r($json);
i get nothing :/
Any thouhgts? I’m sure it something obvious and due to my missunderstanding between arrays and objects with json.
Thanks
2
Answers
Try casting
$notes
witharray
and expose it as json usingjson_encode
:As I understand from what you supplied, the response from
$notes = $woocommerce->get('orders/108668/notes');
is already an array and you can loop through it and do whatever you like. You don’t need to json_decode it.Moreover, json_decode first parameter is string as the docs says but you supplied an array.