gpt4 book ai didi

PHP 尝试在 WooCommerce Api 2.0.1 中获取订单 ID 创建订单功能

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:55 25 4
gpt4 key购买 nike

我使用 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() );
}
}

?>

最佳答案

这些行对我有用:

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

关于PHP 尝试在 WooCommerce Api 2.0.1 中获取订单 ID 创建订单功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37885993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com