简体   繁体   English

Woocommerce:结帐后将JSON发送到外部服务器

[英]Woocommerce: Send JSON to external sever after checkout

I need your help with woocommerce 3.3. 我需要woocommerce 3.3的帮助。 I need to send JSON with order data after checkout with this format: 我需要在结帐后使用以下格式发送带有订单数据的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>}
]
}

Google didn't help me. Google没有帮助我。 So this is all I have at this moment: 所以这就是我现在所拥有的:

<?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 );

To get the Products use this: 要获得产品,请使用以下命令:

$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
}

Than put your data in an array with your format and use "json_decode()" 比将您的数据放入具有您格式的数组中并使用“ json_decode()”

Edit: put your data like this in an array: 编辑:将您的数据放入数组中:

$my_array = array(
'partner' => array(
                'aid' => '123',
                'passwd' => '123',
            ),
'customer' => array(
                'client_name' => 'abs',
                'client_lastname' => '123'
            ),
 // ....and so on ....
);

than convert the array to json: 比将数组转换为json:

$to_json = json_encode($my_array);

check the result: 检查结果:

echo '<pre>';
var_dump($to_json);
echo '</pre>';

it should looks like this: 它应该看起来像这样:

string(97) "{
    "partner":{
        "aid":"123",
         "passwd":"123"
    },
    "customer":{
        "client_name":"abs",
        "client_lastname":"123"
    }
}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM