我使用woocommerce rest API访问woocommerce网站上的订单信息。
数据以json的形式返回,我可以通过这个循环访问所有数据:
$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>';
}
}
第一行是获取订单列表的API调用。
这允许我从第一个对象输出数据而不指定order_number。但是,当访问stdObject中的数组时,例如&#39; line_items&#39;我必须定义一个订单号。为了解决这个问题,我在foreach内部进行了另一次api调用,这次指定了访问信息的订单号。
$order_data = $connect->orders->get($order_number->order_number);
问题是第二次API调用会大大减慢整个页面的速度。
有没有办法让我把它减少到单个api调用来加速页面渲染。
作为参考,这是我正在访问的数据:
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
答案 0 :(得分:1)
根据他们的规格(http://woothemes.github.io/woocommerce-rest-api-docs/#view-list-of-orders),您应该已经拥有$ order结果中的所有信息。