通过 cURL 将数据发送到外部订单管理系统

我正在为 woocommerce 销售手机批发的客户开发一个主题。客户拥有 mobileshop.bz 的帐户,并且他们有自己的系统,称为 NATM。我能够非常轻松地导入产品,但我需要找到一种方法将订单详细信息从我的客户网站发送到他在 mobileshop 上的帐户。


看来我必须先预订文章,然后创建销售订单,mobileshop 的那个人为我提供了这个代码片段来预订文章


    <?php


$curl = curl_init();


curl_setopt_array($curl, array(

  CURLOPT_URL => "https://restful.mobileshop.bz/reserveArticle/new/",

  CURLOPT_RETURNTRANSFER => true,

  CURLOPT_ENCODING => "",

  CURLOPT_MAXREDIRS => 10,

  CURLOPT_TIMEOUT => 0,

  CURLOPT_FOLLOWLOCATION => true,

  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

  CURLOPT_CUSTOMREQUEST => "POST",

  CURLOPT_POSTFIELDS => array('sku' => '','qty' => ''),

  CURLOPT_HTTPHEADER => array(

    "Authorization: paste in your API key"

  ),

));


$response = curl_exec($curl);


curl_close($curl);

echo $response;

这是为了创建SalesOrder


<?php


$curl = curl_init();


curl_setopt_array($curl, array(

  CURLOPT_URL => "https://restful.mobileshop.bz/createSalesOrder/",

  CURLOPT_RETURNTRANSFER => true,

  CURLOPT_ENCODING => "",

  CURLOPT_MAXREDIRS => 10,

  CURLOPT_TIMEOUT => 0,

  CURLOPT_FOLLOWLOCATION => true,

  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

  CURLOPT_CUSTOMREQUEST => "POST",

  CURLOPT_POSTFIELDS => array('reservation[]' => '','reservation[]' => '','pay_method' => '','insurance' => '','drop_shipping' => '0','drop_ship[name]' => '','drop_ship[address]' => '','drop_ship[postcode]' => '','drop_ship[city]' => '','drop_ship[country]' => '','drop_ship[contact]' => ''),

  CURLOPT_HTTPHEADER => array(

    "Authorization: paste in your API key"

  ),

));


$response = curl_exec($curl);


curl_close($curl);

echo $response;

我只是问如何将其集成到我的自定义主题本身中,我是否修改代码示例并将其添加到我的functions.php 文件中。


翻过高山走不出你
浏览 84回答 1
1回答

幕布斯6054654

如果您希望在每个订单上触发此功能,请使用 WooCommerces 挂钩之一并将其放置在您的functions.php文件中。add_action( 'woocommerce_thankyou', 'so_woocommerce_thankyou' );function so_woocommerce_thankyou( $order_id ) {&nbsp; &nbsp; $Order = new WP_Order( $order_id );&nbsp; &nbsp; // Build your item sku and qty array&nbsp; &nbsp; $payloadItems = [];&nbsp; &nbsp; foreach( $Order->get_items() as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; $product = wc_get_product( $item->get_product_id );&nbsp; &nbsp; &nbsp; &nbsp; $payloadItems[] = [$product->get_sku(), $item->get_quantity()];&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Reserve&nbsp; &nbsp; $reservations = [];&nbsp; &nbsp; if( count( $payloadItems ) ) {&nbsp; &nbsp; &nbsp; &nbsp; foreach( $payloadItems as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $reservations[] = reserveArticle( $item[0], $item[1] );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Send sales order&nbsp; &nbsp; $salesOrder = false;&nbsp; &nbsp; if( count( $reservations ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $salesOrder = sendSalesOrder( $Order, $reservations );&nbsp; &nbsp; }&nbsp; &nbsp; if( $salesOrder !== false ) {&nbsp; &nbsp; &nbsp; // Success&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; // Something went wrong&nbsp; &nbsp; }}function reserveArticle( $sku, $qty ) {&nbsp; &nbsp; // The cURL request to reserve an article.&nbsp; &nbsp; // Pipe in the required information into your postfields value return response}function sendSalesOrder( $reservation, $Order ) {&nbsp; &nbsp; // The cURL request to send a sales order.&nbsp; &nbsp; // Pipe in the required information into your postfields value return response or false on error}我就是这样处理的。可能需要根据特定需求和任何错误进行调整,因为它完全没有经过测试
打开App,查看更多内容
随时随地看视频慕课网APP