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

如何在WooCommerce发送付款相关邮件时触发执行指定外部URL

WooCommerce订阅模块发邮件时自动调用外部URL的实现方案

实现原理

WooCommerce及订阅扩展(WooCommerce Subscriptions)所有邮件发送、支付状态变更动作都会触发官方预留的动作钩子,我们只需要挂载自定义函数到对应钩子上,在函数内执行外部URL调用、处理返回的订单状态即可,不需要修改原有核心代码,版本升级不会丢失自定义配置。

具体操作步骤

  • 步骤1:选择匹配场景的触发钩子
    如果你只需要在支付完成类邮件发送时触发调用,优先使用woocommerce_subscription_payment_complete钩子,触发时机和支付流程完全匹配;如果你需要严格在邮件发送成功后才触发调用,可选择通用钩子woocommerce_email_sent,同时可以自行筛选要触发的邮件类型。
  • 步骤2:添加自定义代码
    将以下代码添加到WordPress站点当前使用主题的functions.php文件末尾,或者放入你自行创建的自定义插件中:
    // 支付完成场景触发示例
    add_action( 'woocommerce_subscription_payment_complete', 'call_payment_complete_url_after_subscription_payment', 10, 2 );
    function call_payment_complete_url_after_subscription_payment( $subscription, $last_order ) {
        // 配置要调用的外部URL
        $target_url = 'https://payment-complete.com';
        // 组装要传递的参数,可根据接口需求调整
        $request_args = array(
            'body' => array(
                'order_id' => $last_order->get_id(),
                'subscription_id' => $subscription->get_id(),
                'customer_id' => $subscription->get_user_id(),
            ),
            'timeout' => 3, // 设置短超时,避免接口异常阻塞站点流程
            'blocking' => true, // 如果不需要等待返回结果可以设为false,不阻塞后续流程
        );
        // 发起POST请求
        $response = wp_remote_post( $target_url, $request_args );
        // 判断请求是否成功
        if ( is_wp_error( $response ) ) {
            // 可自行添加错误日志逻辑
            return;
        }
        // 获取接口返回的最新订单状态
        $response_body = json_decode( wp_remote_retrieve_body( $response ), true );
        if ( isset( $response_body['new_order_status'] ) && in_array( $response_body['new_order_status'], array( 'completed', 'processing', 'on-hold' ) ) ) {
            // 更新订单状态
            $last_order->update_status( $response_body['new_order_status'] );
        }
    }
    
    如果你需要严格在邮件发送成功后触发,可使用以下代码:
    add_action( 'woocommerce_email_sent', 'call_external_url_after_subscription_email', 10, 3 );
    function call_external_url_after_subscription_email( $email_id, $email_object, $email ) {
        // 筛选只触发订阅相关的支付类邮件,可自行增减匹配的邮件ID
        $target_email_ids = array( 'customer_completed_order', 'customer_completed_renewal_order', 'customer_processing_renewal_order' );
        if ( !in_array( $email_id, $target_email_ids ) ) {
            return;
        }
        // 获取订单对象
        $order = $email->object;
        if ( !is_a( $order, 'WC_Order' ) ) {
            return;
        }
        // 后续请求、状态更新逻辑和上一段代码完全一致,可直接复用
    }
    
  • 步骤3:功能测试
    模拟一笔订阅支付流程,确认邮件发送的同时外部URL收到请求,订单状态可以正常按照返回结果更新即可。

注意事项

  • 如果不需要阻塞等待接口返回,可将代码中的blocking参数设置为false,请求会异步执行,不会影响邮件发送速度和站点稳定性。
  • 可根据你的实际需求调整匹配的邮件ID,比如要在续费提醒邮件发送时触发,就把续费提醒邮件的ID加入$target_email_ids数组即可。
  • 不要直接修改WooCommerce或订阅扩展的核心代码,避免后续版本升级覆盖你的自定义逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 03:57:01