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

如何在functions.php中认证WooCommerce REST API实现跨店订单同步

解决WooCommerce REST API同步订单的认证问题

Hey there! 看了你的代码,问题核心确实出在WooCommerce REST API的认证环节——你现在的请求完全没带上目标店铺的Consumer Key和Consumer Secret,目标店铺根本不会认可这个请求,自然没法同步订单啦。

先给你理清WooCommerce REST API的认证逻辑:它默认采用HTTP Basic认证,需要把你在目标店铺生成的Consumer Key和Consumer Secret组合成{key}:{secret}的字符串,再通过Base64编码后,放到请求头的Authorization字段里。

下面是修正后的完整代码,我会标注关键修改点:

add_action( 'woocommerce_payment_complete', 'my_api_call');
function my_api_call( $order_id ){
    // Order Setup Via WooCommerce
    $order = new WC_Order( $order_id );
    // Iterate Through Items
    $items = $order->get_items();
    
    // --- 新增:目标店铺的API认证信息 ---
    $consumer_key = '你的目标店铺Consumer Key'; // 替换成你实际的Key
    $consumer_secret = '你的目标店铺Consumer Secret'; // 替换成你实际的Secret
    // 生成Basic认证头部
    $auth_header = base64_encode( $consumer_key . ':' . $consumer_secret );

    foreach ( $items as $item ) {
        // Store Product ID
        $product_id = $item['product_id'];
        $product = new WC_Product($product_id);
        // Check for "categoryx" Category and Run
        if ( has_term( 'categoryx', 'product_cat', $product_id ) ) {
            $name = $order->get_billing_first_name(); // 建议用官方方法代替直接访问属性
            $surname = $order->get_billing_last_name();
            $email = $order->get_billing_email();
            $projectsku = $product->get_sku();
            $apikey ="astringoflettersandnumbers"; // 这个如果是目标店需要的自定义字段可以保留

            // API Callout to URL
            $url = 'https://mystagingsite/wp-json/wc/v3/orders/';
            
            // --- 注意:如果目标是标准Woo店铺,建议用官方订单结构 ---
            // 你之前的自定义字段如果是目标店的特殊要求可以保留,否则换成下面的标准结构
            $body = array(
                // 自定义字段(如果目标店需要的话)
                // "Project" => $projectsku,
                // "Name" => $name,
                // "Surname" => $surname,
                // "Email" => $email,
                // "KEY" => $apikey

                // 标准WooCommerce订单结构示例
                'status' => 'processing', // 设置订单状态
                'billing' => array(
                    'first_name' => $name,
                    'last_name' => $surname,
                    'email' => $email
                ),
                'line_items' => array(
                    array(
                        'sku' => $projectsku,
                        'quantity' => $item->get_quantity() // 从原订单获取商品数量
                    )
                )
            );

            $response = wp_remote_post( $url, array(
                'headers' => array(
                    'Content-Type' => 'application/json; charset=utf-8',
                    'Authorization' => 'Basic ' . $auth_header // --- 新增:认证头部 ---
                ),
                'method' => 'POST',
                'timeout' => 75,
                'body' => json_encode($body),
            ));

            // --- 新增:调试代码,方便排查问题 ---
            if ( is_wp_error( $response ) ) {
                // 把错误写入WP错误日志(可以在wp-content/debug.log查看,需要开启WP_DEBUG)
                error_log( 'Woo API同步错误: ' . $response->get_error_message() );
            } else {
                $response_code = wp_remote_retrieve_response_code( $response );
                error_log( 'API响应状态码: ' . $response_code );
                error_log( 'API响应内容: ' . wp_remote_retrieve_body( $response ) );
            }
        }
    }
}

几个关键提醒:

  • 获取正确的Consumer Key/Secret:在目标Woo店铺后台,进入WooCommerce > 设置 > 高级 > REST API,新建一个应用,选择"读/写"权限,生成后复制Key和Secret。
  • 订单结构匹配:如果目标是标准WooCommerce店铺,一定要用官方要求的订单字段结构(比如billing、line_items),自定义字段可能无法被识别创建订单。如果目标店是自定义接口,再保留你原来的body结构。
  • 调试排查:开启WordPress的WP_DEBUG(在wp-config.php里设置define('WP_DEBUG', true); define('WP_DEBUG_LOG', true);),然后查看wp-content/debug.log里的错误信息,能帮你快速定位问题(比如权限不足、字段错误等)。
  • 权限检查:确保你的Consumer Key拥有创建订单的权限(必须选"读/写",只读权限无法创建订单)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:18:55