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

WooCommerce:向PayPal发送订单追踪信息的方法咨询

解决方案:WooCommerce订单追踪信息同步到PayPal以解锁放款

核心实现思路

你的场景是:WooCommerce已用Shipment Tracking插件管理追踪信息,但需要自动同步这些信息到PayPal,触发PayPal的放款审核。可以通过PayPal Order Management API结合WooCommerce的钩子来实现自动同步,无需额外插件(或替代找轻量同步插件)。

具体步骤

1. 准备PayPal API凭证

  • 登录PayPal商家后台,启用REST API访问,获取Client ID和Secret(先在沙箱环境测试,再切换生产)。
  • 确保API权限包含:write:orders(修改订单)、write:tracking(添加追踪信息)。

2. 用WooCommerce钩子触发同步

Shipment Tracking插件在添加/更新追踪号时会触发wc_shipment_tracking_add_tracking_number钩子,通过这个钩子调用PayPal API推送数据。

将以下代码添加到主题的functions.php或自定义插件中:

// 监听追踪信息添加事件,同步到PayPal
add_action( 'wc_shipment_tracking_add_tracking_number', 'sync_woocommerce_tracking_to_paypal', 10, 4 );

function sync_woocommerce_tracking_to_paypal( $order_id, $tracking_provider, $tracking_number, $date_shipped ) {
    $order = wc_get_order( $order_id );
    $paypal_transaction_id = $order->get_transaction_id();

    // 无PayPal交易ID则跳过
    if ( empty( $paypal_transaction_id ) ) return;

    // 1. 获取PayPal访问令牌
    $client_id = 'YOUR_PAYPAL_CLIENT_ID'; // 替换为你的ID
    $secret = 'YOUR_PAYPAL_SECRET'; // 替换为你的密钥
    $token_response = wp_remote_post( 'https://api-m.paypal.com/v1/oauth2/token', array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret ),
            'Content-Type' => 'application/x-www-form-urlencoded'
        ),
        'body' => 'grant_type=client_credentials'
    ));

    if ( is_wp_error( $token_response ) ) {
        error_log( 'PayPal Token Fetch Error: ' . $token_response->get_error_message() );
        return;
    }

    $token_data = json_decode( wp_remote_retrieve_body( $token_response ), true );
    if ( empty( $token_data['access_token'] ) ) return;

    // 2. 构建追踪信息请求体
    $tracking_payload = array(
        'tracking_number' => $tracking_number,
        'carrier' => map_wc_carrier_to_paypal( $tracking_provider ),
        'shipment_date' => date( 'Y-m-d', strtotime( $date_shipped ) ),
        'items' => array()
    );

    // 追加订单商品信息(PayPal可选,但有助于审核)
    foreach ( $order->get_items() as $item ) {
        $tracking_payload['items'][] = array(
            'name' => $item->get_name(),
            'quantity' => $item->get_quantity()
        );
    }

    // 3. 发送请求到PayPal API
    $api_endpoint = 'https://api-m.paypal.com/v2/checkout/orders/' . $paypal_transaction_id . '/tracking';
    $sync_response = wp_remote_post( $api_endpoint, array(
        'headers' => array(
            'Authorization' => 'Bearer ' . $token_data['access_token'],
            'Content-Type' => 'application/json'
        ),
        'body' => json_encode( $tracking_payload )
    ));

    // 记录日志以便排查问题
    if ( is_wp_error( $sync_response ) ) {
        error_log( 'PayPal Tracking Sync Failed: ' . $sync_response->get_error_message() );
    } else {
        error_log( 'PayPal Tracking Sync Success. Response: ' . wp_remote_retrieve_body( $sync_response ) );
    }
}

// 辅助函数:将WooCommerce物流商名称映射为PayPal认可的代码
function map_wc_carrier_to_paypal( $wc_carrier ) {
    $carrier_mapping = array(
        'USPS' => 'USPS',
        'UPS' => 'UPS',
        'FedEx' => 'FEDEX',
        'DHL' => 'DHL',
        // 根据你实际使用的物流商补充映射,未匹配的用'OTHER'
    );
    return isset( $carrier_mapping[$wc_carrier] ) ? $carrier_mapping[$wc_carrier] : 'OTHER';
}

3. 测试与验证

  • 用PayPal沙箱创建测试订单,添加追踪信息后,检查PayPal沙箱后台是否收到追踪数据,确认是否触发放款。
  • 查看WooCommerce日志(WooCommerce > 状态 > 日志)排查API请求失败原因。

替代方案:使用现成插件

如果不想编写代码,可在WooCommerce插件市场搜索PayPal Tracking Sync类插件,部分插件支持将Shipment Tracking的信息自动同步到PayPal,注意选择适配当前WooCommerce版本、有近期更新的插件。

关键注意事项

  • 物流商名称必须正确映射到PayPal官方支持的代码(可参考PayPal API文档中的物流商列表),否则PayPal可能无法识别。
  • 确保WooCommerce订单的“交易ID”字段已正确记录PayPal的交易ID,否则无法关联订单。
  • 服务器需支持HTTPS及wp_remote_post函数(大部分主机默认支持)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 03:00:04