通过 cURL 将数据发送到外部订单管理系统
Sending Data via cURL to External Order Management System
我正在为使用 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;
然后创建销售订单
<?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 文件中。
非常感谢,
菲利普·杜斯
我可以从两个方面看出这是一体的。
- 使用 JS 并进行 AJAX 调用,完成后即可提交表单。
- 将 Curl 请求封装在方法上,并在用户按下提交时将它们放入。
不是一个具体的想法,但希望它可以让您了解如何处理它。
如果您希望它在每个订单上触发,请使用 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
}
这就是我的处理方式。可能需要针对特定需求和任何错误进行调整,因为它完全没有经过测试。
我正在为使用 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;
然后创建销售订单
<?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 文件中。
非常感谢, 菲利普·杜斯
我可以从两个方面看出这是一体的。
- 使用 JS 并进行 AJAX 调用,完成后即可提交表单。
- 将 Curl 请求封装在方法上,并在用户按下提交时将它们放入。
不是一个具体的想法,但希望它可以让您了解如何处理它。
如果您希望它在每个订单上触发,请使用 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
}
这就是我的处理方式。可能需要针对特定需求和任何错误进行调整,因为它完全没有经过测试。