2

From triggering this Webhook, I receive an order object that looks like this when I output it in an email:

Array{"id":1154,"parent_id":0,"status":"pending","currency":"EUR","version":"3.2.3","prices_include_tax":true,"date_created":{"date":"2017-12-15 15:58:42.000000","timezone_type":1,"timezone":"+00:00"},"date_modified":{"date":"2017-12-15 15:58:42.000000","timezone_type":1,"timezone":"+00:00"},"discount_total":"0","discount_tax":"0","shipping_total":"0","shipping_tax":"0","cart_tax":"0","total":"6.50","total_tax":"0","customer_id":0,"order_key":"wc_order_5a33f1321ba43","billing":{"first_name":"Peter","last_name":"Parker","company":"","address_1":"Baker Str.","address_2":"2","city":"London","state":"","postcode":"50668","country":"DE","email":"[email protected]","phone":"01627423"},"shipping":{"first_name":"Peter","last_name":"Parker","company":"","address_1":"Baker Str.","address_2":"2","city":"London","state":"","postcode":"50668","country":"DE"},"payment_method":"cod","payment_method_title":"Bei Abholung","transaction _id":"","customer_ip_address":"...","customer_user_agent":"mozilla/5.0 (macintosh; intel mac os x 10_13_2) applewebkit/537.36 (khtml, like gecko) chrome/blabla safari/537.36","created_via":"checkout","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"be97db19eba58864b9166961ce22a706","number":"1154","meta_data":[{"id":4731,"key":"_billing_title","value":"1"},{"id":4732,"key":"_shipping_title","value":"1"}],"line_items":{"18":{}},"tax_lines":[],"shipping_lines":{"19":{}},"fee_lines":[],"coupon_lines":[]}

Could you please point me towards a possibility to access elements of this JSON so I can structure a message from certain parts of the $order object? I tried $data = json_decode($order) combined with $data->date_created but that didn't give me anything when I dumped the last part.

2 Answers 2

3

It is json, except the Array part at the start (presuming thats your attempt to access date_created):

You would use json_decode() as you were doing but date_created is also an array, so you would need to also choose which item you want from it.

$data = json_decode($order, true);

echo $data['date_created']['date']; // 2017-12-15 15:58:42.000000

So if you want customer name for example:

echo $data['billing']['first_name'].' '.$data['billing']['last_name'];

By print_r($data)'ing it you can see how its structured.

Array
(
    [id] => 1154
    [parent_id] => 0
    [status] => pending
    [currency] => EUR
    [version] => 3.2.3
    [prices_include_tax] => 1
    [date_created] => Array
        (
            [date] => 2017-12-15 15:58:42.000000
            [timezone_type] => 1
            [timezone] => +00:00
        )

    [date_modified] => Array
        (
            [date] => 2017-12-15 15:58:42.000000
            [timezone_type] => 1
            [timezone] => +00:00
        )

    [discount_total] => 0
    [discount_tax] => 0
    [shipping_total] => 0
    [shipping_tax] => 0
    [cart_tax] => 0
    [total] => 6.50
    [total_tax] => 0
    [customer_id] => 0
    [order_key] => wc_order_5a33f1321ba43
    [billing] => Array
        (
            [first_name] => Peter
            [last_name] => Parker
            [company] => 
            [address_1] => Baker Str.
            [address_2] => 2
            [city] => London
            [state] => 
            [postcode] => 50668
            [country] => DE
            [email] => [email protected]
            [phone] => 01627423
        )

    [shipping] => Array
        (
            [first_name] => Peter
            [last_name] => Parker
            [company] => 
            [address_1] => Baker Str.
            [address_2] => 2
            [city] => London
            [state] => 
            [postcode] => 50668
            [country] => DE
        )

    [payment_method] => cod
    [payment_method_title] => Bei Abholung
    [transaction _id] => 
    [customer_ip_address] => ...
    [customer_user_agent] => mozilla/5.0 (macintosh; intel mac os x 10_13_2) applewebkit/537.36 (khtml, like gecko) chrome/blabla safari/537.36
    [created_via] => checkout
    [customer_note] => 
    [date_completed] => 
    [date_paid] => 
    [cart_hash] => be97db19eba58864b9166961ce22a706
    [number] => 1154
    [meta_data] => Array
        (
            [0] => Array
                (
                    [id] => 4731
                    [key] => _billing_title
                    [value] => 1
                )

            [1] => Array
                (
                    [id] => 4732
                    [key] => _shipping_title
                    [value] => 1
                )

        )

    [line_items] => Array
        (
            [18] => Array
                (
                )

        )

    [tax_lines] => Array
        (
        )

    [shipping_lines] => Array
        (
            [19] => Array
                (
                )

        )

    [fee_lines] => Array
        (
        )

    [coupon_lines] => Array
        (
        )

)

If you want a simple output you can traverse the array with a recursive function which will generate your email.

echo '<h2>Order Details</h2>'.PHP_EOL;
echo order_details(json_decode($json, true));

function order_details($items, $str = null) {   
    foreach ($items as $key => $item) {
        if (is_array($item)) {
            $str .= PHP_EOL.'<h3>'.ucwords(str_replace('_', ' ', $key)).'</h3>'.PHP_EOL;
            $str .= order_details($item, $str);
        } else {
            $str .= '<b>'.ucwords(str_replace('_', ' ', $key)).'</b>: '.$item.'</br>'.PHP_EOL;
        }
    }
    return $str;
}

Which will output like the following: https://3v4l.org/l9BS4

<h2>Order Details</h2>
<b>Id</b>: 1154</br>
<b>Parent Id</b>: 0</br>
<b>Status</b>: pending</br>
<b>Currency</b>: EUR</br>
<b>Version</b>: 3.2.3</br>
<b>Prices Include Tax</b>: 1</br>

<h3>Date Created</h3>
<b>Date</b>: 2017-12-15 15:58:42.000000</br>
<b>Timezone Type</b>: 1</br>
<b>Timezone</b>: +00:00</br>

<h3>Date Modified</h3>
<b>Date</b>: 2017-12-15 15:58:42.000000</br>
<b>Timezone Type</b>: 1</br>
<b>Timezone</b>: +00:00</br>
<b>Discount Total</b>: 0</br>
<b>Discount Tax</b>: 0</br>
<b>Shipping Total</b>: 0</br>
<b>Shipping Tax</b>: 0</br>
<b>Cart Tax</b>: 0</br>
<b>Total</b>: 6.50</br>
<b>Total Tax</b>: 0</br>
<b>Customer Id</b>: 0</br>
<b>Order Key</b>: wc_order_5a33f1321ba43</br>

<h3>Billing</h3>
<b>First Name</b>: Peter</br>
<b>Last Name</b>: Parker</br>
<b>Company</b>: </br>
<b>Address 1</b>: Baker Str.</br>
<b>Address 2</b>: 2</br>
<b>City</b>: London</br>
<b>State</b>: </br>
<b>Postcode</b>: 50668</br>
<b>Country</b>: DE</br>
<b>Email</b>: [email protected]</br>
<b>Phone</b>: 01627423</br>

<h3>Shipping</h3>
<b>First Name</b>: Peter</br>
<b>Last Name</b>: Parker</br>
<b>Company</b>: </br>
<b>Address 1</b>: Baker Str.</br>
<b>Address 2</b>: 2</br>
<b>City</b>: London</br>
<b>State</b>: </br>
<b>Postcode</b>: 50668</br>
<b>Country</b>: DE</br>
<b>Payment Method</b>: cod</br>
<b>Payment Method Title</b>: Bei Abholung</br>
<b>Transaction  Id</b>: </br>
<b>Customer Ip Address</b>: ...</br>
<b>Customer User Agent</b>: mozilla/5.0 (macintosh; intel mac os x 10_13_2) applewebkit/537.36 (khtml, like gecko) chrome/blabla safari/537.36</br>
<b>Created Via</b>: checkout</br>
<b>Customer Note</b>: </br>
<b>Date Completed</b>: </br>
<b>Date Paid</b>: </br>
<b>Cart Hash</b>: be97db19eba58864b9166961ce22a706</br>
<b>Number</b>: 1154</br>

<h3>Meta Data</h3>

<h3>0</h3>
<b>Id</b>: 4731</br>
<b>Key</b>: _billing_title</br>
<b>Value</b>: 1</br>

<h3>1</h3>
<b>Id</b>: 4732</br>
<b>Key</b>: _shipping_title</br>
<b>Value</b>: 1</br>

<h3>Line Items</h3>

<h3>18</h3>

<h3>Tax Lines</h3>

<h3>Shipping Lines</h3>

<h3>19</h3>

<h3>Fee Lines</h3>

<h3>Coupon Lines</h3>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Array is part of the output, not part of my attempt. I simply added $order as message body of an email. I assume that's part of the problem why I can't access the JSON properly!?
You should check if it's there within the string of $order, if it is you should find where its being added and fix it, as it shouldnt be there, if its not json_decode($order, true) should return an array, if it is, it will return null. If you cant remove it for some reason you could always do a work around by checking if its there, and if it is strip it out - 3v4l.org/Hule3
1

When you use

$data = json_decode($order);

it will create an object, to convert this object into an associative array you just need to pass an argument

$data = json_decode($order, true);

Comments

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.