Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

在 WooCommerce 中监听结算表单提交后触发 API 请求

在 WooCommerce 中监听结算表单提交后触发 API 请求通常涉及挂钩到 WooCommerce 提供的特定动作(action)钩子。结算过程中的一个关键钩子是 woocommerce_checkout_order_processed,它在订单数据被处理且订单已经被创建后触发。

这个钩子是处理结算表单提交并触发 API 的理想选择,因为它提供了订单的详细信息,包括订单ID、提交的表单数据和订单对象。

实现步骤

1. 挂钩到 woocommerce_checkout_order_processed

在你的主题的 functions.php 文件或自定义插件中,添加以下代码来挂钩你的自定义函数:

add_action( 'woocommerce_checkout_order_processed', 'trigger_api_after_checkout', 10, 3 );

2. 定义处理 API 请求的函数

在这个函数中,你可以使用提供的参数(订单ID和订单对象)来获取所需的数据,并发送 API 请求。

function trigger_api_after_checkout( $order_id, $posted_data, $order ) {
    $order_data = $order->get_data(); // 获取订单数据

    // 收集客户信息
    $billing = $order_data['billing'];
    $shipping = $order_data['shipping'];

    // 准备API请求的数据
    $api_data = array(
        'id' => $order_id,
        'email' => $billing['email'],
        'phone' => $billing['phone'],
        'country_code' => $billing['country'],
        'sn' => $order_data['order_key'], // 使用订单键作为预生成订单编号
        'token' => '', // 根据您的系统生成或获取
        'cart_token' => WC()->session->get_customer_id(),
        'abandoned_checkout_url' => wc_get_checkout_url(),
        'status' => $order->get_status(),
        'currency' => $order_data['currency'],
        'total_price' => $order_data['total'],
        'created_at' => $order->get_date_created()->getTimestamp(),
        'updated_at' => $order->get_date_modified()->getTimestamp(),
        'line_items' => array(),
    );

    // 添加购物车项目
    foreach ( $order->get_items() as $item_id => $item ) {
        $product = $item->get_product();
        $api_data['line_items'][] = array(
            'id' => $item_id,
            'src' => wp_get_attachment_image_src($product->get_image_id(), 'full')[0],
            'name' => $item->get_name(),
            'sku' => $product->get_sku(),
            'variant_title' => $item->get_variation_id() ? wc_get_formatted_variation(wc_get_product($item->get_variation_id()), true) : '',
            'variant_id' => $item->get_variation_id() ?: null,
            'quantity' => $item->get_quantity(),
            'price' => $item->get_subtotal(), // 或 $item->get_total()
            'line_price' => $item->get_total(),
            'product_id' => $product->get_id(),
            'product_url' => get_permalink($product->get_id()),
            'title' => $product->get_title(),
        );
    }

    // 发送API请求
    $api_url = '你的API端点';
    $response = wp_remote_post($api_url, array(
        'headers'     => array('Content-Type' => 'application/json; charset=utf-8'),
        'body'        => json_encode($api_data),
        'method'      => 'POST',
        'data_format' => 'body',
    ));

    // 处理响应
    // ...
}
add_action( 'woocommerce_checkout_order_processed', 'trigger_api_after_checkout', 10, 3 );

3. 测试和调试

在部署到生产环境之前,彻底测试此功能以确保它按照预期工作,尤其是在处理订单数据和发送API请求时。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论