我需要你对woocommerce 3.3的帮助。 我需要在结账后使用以下格式发送带有订单数据的JSON:
json = {
“partner”:
{
“aid”:<APIUserID>,
“password”:<APIUserPass>
},
“customer”:
{
“client_name”:<CustomerName>,
“client_lastname”:<CustomerLastName>,
“client_phone”:<7-999-9999999>,
“client_email”:<email>,
“promo”:< coupon_codes>
},
“items”:
[
{“prod_id”:<ProductID_1>,”quantity”:<Quantity_1>},
…
{“prod_id”:<ProductID_2>,”quantity”:<Quantity_2>}
]
}
谷歌没有帮助我。
所以这就是我现在所拥有的一切:
<?php
function action_woocommerce_new_order( $order_id ) {
$order = wc_get_order( $order_id );
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_phone();
$order->get_billing_email();
// I need to get products list
// And I need to send this to another server with JSON
};
// add the action
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 3 );
答案 0 :(得分:0)
要获得产品,请使用:
$line_items = $order->get_items();
foreach ( $line_items as $item ) {
// product
$product = $order->get_product_from_item( $item );
// now you can get the data from $product array
}
将数据放入带有格式的数组中,然后使用“json_decode()”
编辑: 把你的数据放在一个数组中:
$my_array = array(
'partner' => array(
'aid' => '123',
'passwd' => '123',
),
'customer' => array(
'client_name' => 'abs',
'client_lastname' => '123'
),
// ....and so on ....
);
将数组转换为json:
$to_json = json_encode($my_array);
检查结果:
echo '<pre>';
var_dump($to_json);
echo '</pre>';
它应该是这样的:
string(97) "{
"partner":{
"aid":"123",
"passwd":"123"
},
"customer":{
"client_name":"abs",
"client_lastname":"123"
}
}"