0

I am using woocommerce 2.6 now on woocommerce 3.0 output of $item->get_items has changed from Array to Objects.

I want to convert object std class to array by key name to fetch data. Below is output of custom woocommerce 3.x vardump.

Please help me on this.

[1]=>
object(stdClass)#18913 (3) {
  ["id"]=>
  int(3944)
  ["key"]=>
  string(5) "Vuxen"
  ["value"]=>
  string(15) "2 (4 800,00 Kr)"
}
[2]=>
object(stdClass)#18912 (3) {
  ["id"]=>
  int(3945)
  ["key"]=>
  string(18) "Totalt biljettpris"
  ["value"]=>
  string(11) "4 800,00 Kr"
}
[3]=>
object(stdClass)#18911 (3) {
  ["id"]=>
  int(3946)
  ["key"]=>
  string(30) "jrp_name_adult_ordinary_7day_1"
  ["value"]=>
  string(26) "undefined/kjhjksh/jdhfjshk"
}
[4]=>
object(stdClass)#18910 (3) {
  ["id"]=>
  int(3947)
  ["key"]=>
  string(13) "Nationality_1"
  ["value"]=>
  string(25) "Nej, ej på japanskt pass"
}
[5]=>
object(stdClass)#18909 (3) {
  ["id"]=>
  int(3948)
  ["key"]=>
  string(30) "jrp_name_adult_ordinary_7day_2"
  ["value"]=>
  string(33) "undefined/jhdsfjjhdkjs/jkdshfjshj"
}
[6]=>
object(stdClass)#18908 (3) {
  ["id"]=>
  int(3949)
  ["key"]=>
  string(13) "Nationality_2"
  ["value"]=>
  string(21) "Ja, på japanskt pass"
}
[7]=>
object(stdClass)#18907 (3) {
  ["id"]=>
  int(3950)
  ["key"]=>
  string(11) "Avresedatum"
  ["value"]=>
  string(10) "30/09/2017"
}
[8]=>
object(stdClass)#18906 (3) {
  ["id"]=>
  int(3951)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(65) "Jag har förstått villkoren för användning av Japan Rail Pass."
}
[9]=>
object(stdClass)#18905 (3) {
  ["id"]=>
  int(3952)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(88) "Jag förstår att det är mitt ansvar att se till att jag har korrekt visum i mitt pass."
}
[10]=>
object(stdClass)#18904 (3) {
  ["id"]=>
  int(3953)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(88) "Jag har förstått villkoren för användning av Japan Rail Pass som japansk medborgare."
}
[11]=>
object(stdClass)#18903 (3) {
  ["id"]=>
  int(3954)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(84) "Jag förstår att det är mitt ansvar att se till att jag har korrekt dokumentation."
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Kuanl
  • 23
  • 5

5 Answers5

1

Taken from php stdClass to array

$array = json_decode(json_encode($nameOfStdClassVariable), true);
Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
1

Use this:

$itemsArray = array_map(function($v) {
    return (array)$v;
}, $item->get_items);
kip
  • 1,120
  • 7
  • 11
0

You can try like this,

$array = json_decode(json_encode($item), true);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

When you make the call to the rest API, you can pass one array with params. If you put this param "return_as_array" true they return the values as Array.

$options = array('debug' => false,
            'return_as_array' => true,
            'validate_url' => false,
            'timeout' => 30,
            'ssl_verify' => false);

new WC_API_Client($url, $consumer_key, $consumer_secret, $options);

Link to github repository

Rick
  • 81
  • 5
0

You can use the following code to get all objects to an array.If the key in the objects is found multiple times it will create unique array by key name and fetch all value from all objects to an unique key.

$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_id => $item_obj) {
    $item_data = $item_obj->get_data();
    foreach ($item_data as $key => $object) {
    }
    for($m=0;$m<count($object);$m++) {
        $new_array[$object[$m]->key][$m] = $object[$m]->value;
    }
}
echo "<pre>";var_dump($new_array);echo "</pre>";
gre_gor
  • 6,669
  • 9
  • 47
  • 52