【问题标题】:Sending Order Details From WooCommerce To External System Over API通过 API 从 WooCommerce 向外部系统发送订单详细信息
【发布时间】:2019-08-13 00:40:57
【问题描述】:

我正在尝试通过我编写的外部 api 将 woocommerce 订单发送到 netsuite。我是 woocommerce 的新手,不完全了解如何添加此功能。

我已将以下代码添加到 public_html/wp-content/themes/reverie-master/

中的 functions.php 文件中
add_action( 'woocommerce_payment_complete'', 'wdm_send_order_to_ext'); 
function wdm_send_order_to_ext( $order_id ){
// get order object and order details
$order = new WC_Order( $order_id ); 
$email = $order->billing_email;
$phone = $order->billing_phone;

//Create the data object
$orderData = array(
    'customer_email' => $email,
    'customer_phone' => $phone
);

$apiData = array(
    'caller' => 'woocommerce',
    'json' => $orderData,
    'key' => 'MY_SECRET_KEY'
);

$jsonData =json_encode($orderData);

$url = "";
$api_mode = 'sandbox';
if($api_mode == 'sandbox'){
    // sandbox URL example
    $url = "https://forms.netsuite.com/app/site/hosting/scriptlet.nl?script=XXX&deploy=X&compid=XXXXXXX_SB1&h=XXXXXXXXXXXXXXXX"; 
}
else{
    // production URL example
    $url = ""; 
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($jsonData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec ($ch);

curl_close ($ch);

// the handle response    
if (strpos($response,'ERROR') !== false) {
        print_r($response);
} else {
        // success
}
}

我已经测试了这段代码的主要部分,只是在不同站点中与 woocommerce 无关的部分,我可以在 NetSuite 中看到显示的数据。但是,当我通过我的商店下订单并付款时,我没有看到数据进入 NetSuite。我在正确的位置有此代码吗?有什么我遗漏的吗?

更新 我安装了插件代码片段并在那里添加了代码。将其设置为到处运行 sn-p。还是没有运气。

【问题讨论】:

    标签: woocommerce netsuite


    【解决方案1】:

    您的第一个链接似乎有双引号

    改变

    add_action( 'woocommerce_payment_complete'', 'wdm_send_order_to_ext');

    add_action( 'woocommerce_payment_complete', 'wdm_send_order_to_ext');

    而不是使用 curl - 你总是可以使用 WordPress 中的构建 wp_remote_post() 函数

    还要确保在测试时将wp-config.php 中的WP_DEBUG 设置为true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      相关资源
      最近更新 更多