我使用WooCommerce API创建订单,效果很好。
这就是我所做的 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 :(得分:0)
这些线对我有用:
$result = get_object_vars( $client->orders->create($data) );
echo( get_object_vars(array_values($result)[0])['id'] );