【问题标题】:Sending Data via cURL to External Order Management System通过 cURL 向外部订单管理系统发送数据
【发布时间】:2020-07-18 09:41:09
【问题描述】:

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

看来我得先预约商品再创建销售订单,mobileshop的人给了我这个代码sn-p来预约商品

    <?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 文件中。

非常感谢, 菲利普·杜斯

【问题讨论】:

标签: php curl woocommerce wordpress-theming php-curl


【解决方案1】:

如果您希望在每个订单上触发此功能,请使用 WooCommerces 挂钩之一并将其放入您的 functions.php 文件中。

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
}

我就是这样处理它的。可能需要针对特定​​需求和任何错误进行调整,因为它完全没有经过测试。

【讨论】:

    【解决方案2】:

    我可以看到这是两种方式之一。

    1. 使用 JS 并进行 AJAX 调用,一旦完成,您就可以提交表单了。
    2. 将 Curl 请求封装在方法上,并在用户按下提交时将其放入。

    不是一个具体的想法,但希望它可以让您了解如何处理它。

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      相关资源
      最近更新 更多