为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
翻过高山走不出你 2023-08-11 16:28:11
我正在为 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 文件中。
查看完整描述

1 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

如果您希望在每个订单上触发此功能,请使用 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

}

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


查看完整回答
反对 回复 2023-08-11
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信