You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Woocommerce钩子实现支付完成后发送Gravity Forms字段至API?

问题:WooCommerce支付完成后调用第三方API失败(基于Gravity Forms字段)

我禁用了WooCommerce默认的账单与配送字段,通过Gravity Forms创建了手机号和邮箱字段,并借助Gravity Forms Product Add-ons将这两个字段显示在所有产品页面供用户下单时填写。

此前在仅使用Gravity Forms的旧站点中,我可以通过gform_post_payment_completed钩子实现支付完成后向第三方API发送数据,代码如下:

//Send to API
function gfroms_after_payment_sms( $entry, $action ) {

    // for more info, check https://www.gravityhelp.com/documentation/article/entry-object/

    $url = 'https://thirdparty.com/api/v2/send';
    $apiKey = '00000000000';
    $headers = array('api-key: '.$apiKey);
    $from = 'Sender\'s Name';
    $to = [$entry['14']]; //phone number field ID
    $email = $entry['10.1']; //Email field ID
    $body = "blah blah blah $email and $to from $from"; 
    
    
    $data = array (
       'recipients' => $to,
       'sender' => $from,
       'message' => $body
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    $result = http_build_query($result, TRUE);
    curl_close($ch);
    
}
// Tells the notification to be sent only when this hook is found and to include the arguments ($entry and $action)
add_action( 'gform_post_payment_completed', 'gfroms_after_payment_sms',  10, 2 );

现在我尝试改用WooCommerce的woocommerce_payment_complete钩子实现相同功能,但编写的代码无法正常运行,代码如下:

function custom_msg_customer_process_order( $order_id ) {

    $order = wc_get_order( $order_id );

    // Quit early if not a valid order.
    if ( ! $order ) {
         return;
    }

    $url = 'https://thirdparty.com/api/v2/send';
    $apiKey = '00000000000';
    $headers = array('api-key: '.$apiKey);
    $from = 'Sender\'s Name';
    $to = [$entry['14']]; //phone number field ID
    $email = $entry['10.1']; //Email field ID
    $body = "blah blah blah $email and $to from $from"; 


    $data = array (
       'recipients' => $to,
       'sender' => $from,
       'message' => $body
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $result = curl_exec($ch);
    $result = http_build_query($result, TRUE);
    curl_close($ch);


add_action('woocommerce_payment_complete', 'custom_msg_customer_process_order' );

问题分析

原代码的核心问题有两个:

  1. 未从WooCommerce订单中获取Gravity Forms的字段数据——$entry变量在woocommerce_payment_complete钩子中未定义,无法直接调用。
  2. 函数末尾缺少闭合的},导致语法错误。

修正后的代码

function custom_msg_customer_process_order( $order_id ) {
    $order = wc_get_order( $order_id );

    // 无效订单直接退出
    if ( ! $order ) {
         return;
    }

    // 从订单中获取Gravity Forms条目ID
    $entry_id = $order->get_meta( '_gravity_form_entry_id' );
    if ( empty( $entry_id ) ) {
        return;
    }

    // 获取Gravity Forms条目数据
    $entry = GFAPI::get_entry( $entry_id );
    if ( is_wp_error( $entry ) ) {
        return;
    }

    $url = 'https://thirdparty.com/api/v2/send';
    $apiKey = '00000000000';
    $headers = array('api-key: '.$apiKey);
    $from = 'Sender\'s Name';
    
    // 替换为你的Gravity Forms实际字段ID
    $phone_number = rtrim( $entry['14'], '+' ); // 清理手机号格式(可选)
    $to = [$phone_number]; 
    $email = $entry['10.1']; 
    $body = "blah blah blah $email and $phone_number from $from"; 

    $data = array (
       'recipients' => $to,
       'sender' => $from,
       'message' => $body
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true); // 改用POST常量更规范
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    
    // 添加错误捕获(可选,用于调试)
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        error_log('API请求错误: ' . curl_error($ch));
    }
    
    curl_close($ch);
}
add_action('woocommerce_payment_complete', 'custom_msg_customer_process_order' );

关键修改说明

  • 通过$order->get_meta( '_gravity_form_entry_id' )获取订单关联的Gravity Forms条目ID,再用GFAPI::get_entry()获取完整的字段数据。
  • 修复了函数未闭合的语法错误。
  • 改用CURLOPT_POST替代CURLOPT_CUSTOMREQUEST,符合标准POST请求规范。
  • 增加了错误捕获逻辑,方便调试API请求问题。
  • 对手机号字段做了简单的格式清理(可根据实际需求调整)。

内容的提问来源于stack exchange,提问作者Kobbe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 22:35:22