1

HI I using the woocommerce rest API to access order information made on a woocommerce site.

The data is being returned as json, I can access all of the data with this loop:

$orders = $connect->orders->get();
        $order_numbers = $orders->orders;

        foreach ($order_numbers as $order_number) {            

            $order_data = $connect->orders->get($order_number->order_number);

            echo '</br>' . $order_number->order_number . '</br>';
            echo $order_data->order->total . '</br>';

            $line_items = $order_data->order->line_items;

            foreach($line_items as $item){
                echo $item->name . ' x ' . $item->quantity . '</br>';   
            }

        }

The first line is the API call to get a lists of orders.

This allows me to output the data from the first object without specifying an order_number. However when accessing an array within the stdObject for example 'line_items' I have to define an order number. To overcome this I make another api call inside of the foreach this time specifying the order number to access the information.

$order_data = $connect->orders->get($order_number->order_number);

The problem is that this second API call massively slows down the entire page.

Is there a way for me to reduce this to a single api call to speed up the page rendering.

For reference this is the data I am accessing:

object(stdClass)[15]
  public 'order' => 
    object(stdClass)[16]
      public 'id' => int 22
      public 'order_number' => int 22
      public 'created_at' => string '2015-07-30T14:01:54Z' (length=20)
      public 'updated_at' => string '2015-07-30T14:01:54Z' (length=20)
      public 'completed_at' => string '2015-07-30T13:01:54Z' (length=20)
      public 'status' => string 'on-hold' (length=7)
      public 'currency' => string 'GBP' (length=3)
      public 'total' => string '3.84' (length=4)
      public 'subtotal' => string '3.84' (length=4)
      public 'total_line_items_quantity' => int 2
      public 'total_tax' => string '0.00' (length=4)
      public 'total_shipping' => string '0.00' (length=4)
      public 'cart_tax' => string '0.00' (length=4)
      public 'shipping_tax' => string '0.00' (length=4)
      public 'total_discount' => string '0.00' (length=4)
      public 'shipping_methods' => string '' (length=0)
      public 'payment_details' => 
        object(stdClass)[17]
          public 'method_id' => string 'bacs' (length=4)
          public 'method_title' => string 'Direct Bank Transfer' (length=20)
          public 'paid' => boolean false
      public 'billing_address' => 
        object(stdClass)[18]
          public 'first_name' => string 'Chris' (length=5)
          public 'last_name' => string '#' (length=5)
          public 'company' => string '' (length=0)
          public 'address_1' => string '#' (length=4)
          public 'address_2' => string '' (length=0)
          public 'city' => string '#' (length=7)
          public 'state' => string '' (length=0)
          public 'postcode' => string '#' (length=7)
          public 'country' => string 'GB' (length=2)
          public 'email' => string '#' (length=20)
          public 'phone' => string '#' (length=11)
      public 'shipping_address' => 
        object(stdClass)[19]
          public 'first_name' => string 'Chris' (length=5)
          public 'last_name' => string '#' (length=5)
          public 'company' => string '' (length=0)
          public 'address_1' => string '#' (length=4)
          public 'address_2' => string '' (length=0)
          public 'city' => string '#' (length=7)
          public 'state' => string '' (length=0)
          public 'postcode' => string '#' (length=7)
          public 'country' => string 'GB' (length=2)
      public 'note' => string '' (length=0)
      public 'customer_ip' => string '#' (length=15)
      public 'customer_user_agent' => string 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4' (length=134)
      public 'customer_id' => int 1
      public 'view_order_url' => string '#' (length=58)
      public 'line_items' => 
        array (size=2)
          0 => 
            object(stdClass)[20]
              ...
          1 => 
            object(stdClass)[21]
              ...
      public 'shipping_lines' => 
        array (size=0)
          empty

1 Answer 1

1

According to their specs (http://woothemes.github.io/woocommerce-rest-api-docs/#view-list-of-orders) you should allready have all the information in your $orders result.

Sign up to request clarification or add additional context in comments.

3 Comments

I do but the problem is when accessing the line_items array I have to specify an order ID which can only be done by an API call from what I can see. If I don't specify an ID I get a php invalid index error back. And specifying the ID defeats the object of looping through all the orders.
as far as i can see the order-structure of both the view list orders and view order, the first just being an array of order objects. why don't you try foreach ($order_numbers as $order) { echo '</br>' . $order->order_number . '</br>'; echo $order->total . '</br>'; $line_items = $order->line_items; foreach($line_items as $item){ echo $item->name . ' x ' . $item->quantity . '</br>'; } }
Thank you so much. That woks perfectly. I can see now comparing my code and yours what I was doing wrong. The page now renders instantly!

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.