首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过cURL向外部订单管理系统发送数据

通过cURL向外部订单管理系统发送数据
EN

Stack Overflow用户
提问于 2020-07-18 09:41:09
回答 2查看 493关注 0票数 2

我正在为woocommerce批发销售手机的客户开发一个主题。客户端在mobileshop.bz上有一个帐户,他们有自己的系统,名为NATM。我可以进口的产品真的很容易,但我需要找到一种方式,从我的客户网站发送订单细节到他的帐户上移动。

似乎我必须先预订文章,然后创建一个销售订单,这个家伙在mobileshop为我提供了这段代码片段,以保留一篇文章

代码语言:javascript
复制
    <?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;

这是给createSalesOrder的

代码语言:javascript
复制
<?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文件中。

非常感谢,菲利普·露丝

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-23 21:47:02

如果您希望在每一个订单上启动它,请使用一个WooCommerces钩子并将其放置到您的functions.php文件中。

代码语言:javascript
复制
add_action( 'woocommerce_thankyou', 'so_woocommerce_thankyou' );
function so_woocommerce_thankyou( $order_id ) {
    $Order = new WP_Order( $order_id );
    // Build your item sku and qty array
    $payloadItems = [];
    foreach( $Order->get_items() as $item ) {
        $product = wc_get_product( $item->get_product_id );
        $payloadItems[] = [$product->get_sku(), $item->get_quantity()];
    }
    
    // Reserve
    $reservations = [];
    if( count( $payloadItems ) ) {
        foreach( $payloadItems as $item ) {
            $reservations[] = reserveArticle( $item[0], $item[1] );
        }
    }

    // Send sales order
    $salesOrder = false;
    if( count( $reservations ) ) {
        $salesOrder = sendSalesOrder( $Order, $reservations );
    }

    if( $salesOrder !== false ) {
      // Success
    } else {
      // Something went wrong
    }
}

function reserveArticle( $sku, $qty ) {
    // The cURL request to reserve an article.
    // Pipe in the required information into your postfields value return response
}

function sendSalesOrder( $reservation, $Order ) {
    // The cURL request to send a sales order.
    // Pipe in the required information into your postfields value return response or false on error
}

我就是这样接近它的。可能需要根据特定需求和任何错误进行调整,因为它完全没有经过测试。

票数 1
EN

Stack Overflow用户

发布于 2020-07-23 00:07:28

我可以从两方面看出这是一个整体。

form.

  • Encapsulate
  1. 使用JS并在完成后进行AJAX调用,您可以提交方法上的
  2. Curl请求,并在用户按submit.

时丢弃它们。

这不是一个具体的想法,但希望它能给你提供如何处理它的想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62967013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档