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

WooCommerce:订单状态为completed时在thankyou页面加自定义文本报错求助

问题原因

woocommerce_thankyou 钩子传递的参数是订单ID(整数),而非直接的WC_Order对象,你原来的代码直接把它当作对象调用get_status(),自然会触发错误。

修正后的代码
add_action( 'woocommerce_thankyou', 'complete_custom_text' );

function complete_custom_text( $order_id ) {
    // 跳过无效订单ID
    if ( ! $order_id ) return;

    // 通过订单ID获取WC_Order对象
    $order = wc_get_order( $order_id );

    // 确保订单对象存在
    if ( ! $order ) return;

    $status = $order->get_status();
    if ( $status === 'completed' ) {
        echo '<p><b>Text (Custom) 5:</b> Perfect.</p>';
    }
}
关键说明
  • 用wc_get_order()将订单ID转换为合法的订单对象,这是适配woocommerce_thankyou钩子的核心操作
  • 增加了订单ID和订单对象的有效性检查,避免因异常订单数据导致额外报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 05:30:46