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

WooCommerce订单完成后调用API实现Moodle自动选课求助

在WooCommerce订单完成后自动调用Moodle选课API的实现方案

你可以利用WordPress内置的wp_remote_post()函数来发送JSON请求,无需额外依赖curl。以下是完整的my_order_complete_function函数实现:

function my_order_complete_function( $order_id ) {
    // 1. 获取WooCommerce订单对象
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        error_log( '无法获取订单ID ' . $order_id . ' 的数据' );
        return;
    }

    // 2. 从订单中提取所需数据
    $user_data = array(
        'email'           => $order->get_billing_email(),
        'firstname'       => $order->get_billing_first_name(),
        'lastname'        => $order->get_billing_last_name(),
        // 注意:这里需要根据你的商品与Moodle课程的映射关系获取courseid
        // 示例:假设商品ID对应courseid,可通过订单商品获取
        'courseid'        => get_product_course_id( $order ), // 需自行实现该函数或替换为实际值
        'address1'        => $order->get_billing_address_1(),
        'address2'        => $order->get_billing_address_2(),
        'addresscity'     => $order->get_billing_city(),
        'addresspostcode' => $order->get_billing_postcode(),
        'addresscounty'   => $order->get_billing_state(),
        'addresscountry'  => $order->get_billing_country(), // WooCommerce返回两位国家码,需确认Moodle是否接受(如GB对应GBR)
        'dob'             => $order->get_meta( 'billing_dob', true ) // 假设出生日期存在自定义字段中,替换为实际字段名
    );

    // 3. 构造符合API要求的请求体
    $request_body = array(
        'users' => array( $user_data )
    );

    // 4. 设置请求参数
    $api_url = 'https://XXXX.co.uk/webservice/restful/server.php/XXXX_woo_commerce?moodlewsrestformat=json';
    $args = array(
        'headers' => array(
            'Content-Type' => 'application/json',
            // 如果API需要认证,添加对应头信息,比如:
            // 'Authorization' => 'Bearer YOUR_API_TOKEN'
        ),
        'body'    => wp_json_encode( $request_body ),
        'method'  => 'POST',
        'timeout' => 30, // 设置超时时间,避免阻塞
    );

    // 5. 发送POST请求
    $response = wp_remote_post( $api_url, $args );

    // 6. 处理响应结果(用于调试和日志)
    if ( is_wp_error( $response ) ) {
        $error_message = $response->get_error_message();
        error_log( '调用Moodle API失败:' . $error_message );
    } else {
        $response_code = wp_remote_retrieve_response_code( $response );
        $response_body = wp_remote_retrieve_body( $response );
        if ( $response_code === 200 ) {
            error_log( 'Moodle API调用成功,响应:' . $response_body );
            // 可选:标记订单已同步到Moodle
            $order->update_meta_data( '_moodle_course_enrolled', 'yes' );
            $order->save();
        } else {
            error_log( 'Moodle API返回错误状态码:' . $response_code . ',响应:' . $response_body );
        }
    }
}

// 辅助函数:从订单中获取对应的Moodle课程ID(需根据实际情况调整)
function get_product_course_id( $order ) {
    $course_id = '';
    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id();
        // 示例:从商品自定义字段中读取Moodle课程ID
        $course_id = get_post_meta( $product_id, '_moodle_course_id', true );
        // 多商品订单可调整逻辑,比如只取第一个或批量处理
        break;
    }
    return $course_id;
}

关键注意事项:

  • 课程ID映射:必须实现get_product_course_id函数,将WooCommerce商品与Moodle课程ID关联,比如通过商品自定义字段存储对应courseid。
  • 国家码格式:WooCommerce返回两位国家码(如GB),需确认Moodle API是否接受三位码(如GBR),必要时做格式转换。
  • 认证信息:如果Moodle API需要额外认证,在headers中添加对应字段,比如Bearer Token。
  • 钩子绑定:确保函数绑定到WooCommerce订单完成钩子:
    add_action( 'woocommerce_order_status_completed', 'my_order_complete_function' );
    
  • 日志调试:代码用error_log记录请求结果,可通过WordPress日志文件或调试插件查看信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 16:32:40