skip to Main Content

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


  1. Try casting $notes with array and expose it as json using json_encode:

    $notes = $woocommerce->get('orders/108668/notes');
    header('Content-Type: application/json'); // this line is to make sure the output format is JSON
    echo json_encode((array)$notes);
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search