2016-06-17 107 views
3

我使用WooCommerce API創建訂單,它工作得很好。PHP嘗試在WooCommerce Api 2.0.1中獲取訂單ID創建訂單功能

這就是我所做的print_r($client->orders->create($data)

stdClass Object 
    (
    [order]=> stdClass Object 
     (
     [id]=> 817 
     [order_number]=> 817 
     [created_at]=> 2016-06-15T14:23:30Z 
     [updated_at]=> 2016-06-15T14:23:30Z 
     [completed_at]=> 2016-06-15T14:23:30Z 
     [status]=> pending 
     [currency]=> EUR 
     [total]=> 0.00 
     [subtotal]=> 0.00 
     [total_line_items_quantity]=> 3 
     [total_tax]=> 0.00 
     [total_shipping]=> 0.00 
     [cart_tax]=> 0.00 
     [shipping_tax]=> 0.00 
     [total_discount]=> 0.00 
     [shipping_methods]=> Free Shipping 
     [payment_details]=> stdClass Object 
      (
       [method_id]=> paypal 
       [method_title]=> PayPal and Credit Cards 
       [paid]=> 
      ) 
     [billing_address]=> stdClass Object 
      (

我試圖獲得id值。

我將此功能稱爲創建訂單print_r($client->orders->create($data); 其中$data是一個包含訂單詳細信息的數組。

嘗試過這種方法,但不起作用:

$object = print_r($client->orders->create($data), true); 
echo $object->order->id; 

我用這個API

這是我的PHP代碼:

<?php 
require_once('lib/woocommerce-api.php'); 
$options = array(
    'debug'   => true, 
    'return_as_array' => true, 
    'validate_url' => false, 
    'timeout'   => 30, 
    'ssl_verify'  => false, 
); 

$data = array(
    'order' => array(
     'payment_details' => array(
      'method_id' => 'paypal', 
      'method_title' => 'PayPal and Credit Cards', 
      'paid' => false 
     ), 
     'billing_address' => array(
      'first_name' => 'name', 
      'last_name' => 'surname', 
      'company' => '', 
      'address_1' => 'address', 
      'address_2' => '', 
      'city' => 'city', 
      'state' => 'state', 
      'postcode' => 'cap', 
      'country' => 'UK', 
      'email' => 'email', 
      'phone' => 'phone' 
     ), 
     'shipping_address' => array(
      'first_name' => 'name', 
      'last_name' => 'surname', 
      'company' => '', 
      'address_1' => 'address', 
      'address_2' => '', 
      'city' => 'city', 
      'state' => 'state', 
      'postcode' => 'cap', 
      'country' => 'UK' 
     ), 
     'customer_id' => 0, 
     'line_items' => array(
      array(
       'product_id' => 871, 
       'quantity' => 1 
      ) 
     ), 
     'shipping_lines' => array(
      array(
       'method_id' => 'free_shipping', 
       'method_title' => 'Free Shipping', 
       'total' => 0.00 
      ) 
     ) 
    ) 
); 

try { 

    $client = new WC_API_Client('http://my-site.com', '#####', '#####', $options); 

    $object = print_r($client->orders->create($data), true); 
    echo $object['order']['id']; 

} catch (WC_API_Client_Exception $e) { 
    echo $e->getMessage() . PHP_EOL; 
    echo $e->getCode() . PHP_EOL; 
    if ($e instanceof WC_API_Client_HTTP_Exception) { 
     print_r($e->get_request()); 
     print_r($e->get_response()); 
    } 
} 

?> 
+0

echo $ nameofyourobje ctarray - > order - > id; – SML

+0

如果你讀過我的問題,我已經嘗試過**'echo $ nameofyourobjectarray - > order - > id;'** – Federico

+0

數組是否高於你在print_r $對象時得到的數組? – SML

回答

0

這些線路爲我工作:

$result = get_object_vars($client->orders->create($data)); 
echo(get_object_vars(array_values($result)[0])['id']);