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

使用woocommerce_thankyou钩子添加地址触发致命错误求助

WooCommerce感谢页添加订单地址代码报错修复

问题说明

尝试在WooCommerce感谢页底部添加订单账单地址与收货地址,修改了一段在其他页面可用的PHP代码后触发致命错误。

原代码:

function thank_you_add_address(){
        $order_id = absint( get_query_var( 'order-pay' ) );
        $order = wc_get_order( $order_id );
    
        ?>
        <!-- Display Information -->
        <h2 class="woocommerce-column__title"><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
            <address>
            <?php echo wp_kses_post( $order->get_formatted_billing_address( __( 'N/A', 'woocommerce' ) ) ); ?>
            <?php if ( $order->get_billing_phone() ) : ?>
                <p class="woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>
            <?php endif; ?>
            <?php if ( $order->get_billing_email() ) : ?>
                <p class="woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>
            <?php endif; ?>
        </address>

        <h2 class="woocommerce-column__title"><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
        <address>
            <?php echo wp_kses_post( $order->get_formatted_shipping_address( __( 'N/A', 'woocommerce' ) ) ); ?>
        </address>
        <?php
          }

    add_action('woocommerce_thankyou', 'thank_you_add_address');

错误信息(翻译后):

致命错误:未捕获 Error:在布尔值上调用成员函数 get_formatted_billing_address()
堆栈跟踪:
#0 .../wp-includes/class-wp-hook.php(308): thank_you_add_address(23078)
#1 .../wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array)
#2 .../wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#3 .../woocommerce/templates/checkout/thankyou.php(80): do_action('woocommerce_thankyou', 23078)
#4 .../woocommerce/includes/wc-core-functions.php(345): include('...')
#5 ... 网站发生严重错误。

错误原因

  1. get_query_var('order-pay')是用于订单支付页面的参数,感谢页不会传递这个变量,导致获取的$order_id无效
  2. 无效的订单ID传入wc_get_order()后返回布尔值false,后续调用$order->get_formatted_billing_address()等方法时,就会触发"在布尔值上调用成员函数"的错误

修复后的代码

function thank_you_add_address( $order_id ){
    // 获取订单对象并做有效性判断
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return;
    }
    ?>
    <!-- Display Information -->
    <h2 class="woocommerce-column__title"><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
    <address>
        <?php echo wp_kses_post( $order->get_formatted_billing_address( __( 'N/A', 'woocommerce' ) ) ); ?>
        <?php if ( $order->get_billing_phone() ) : ?>
            <p class="woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>
        <?php endif; ?>
        <?php if ( $order->get_billing_email() ) : ?>
            <p class="woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>
        <?php endif; ?>
    </address>

    <h2 class="woocommerce-column__title"><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
    <address>
        <?php echo wp_kses_post( $order->get_formatted_shipping_address( __( 'N/A', 'woocommerce' ) ) ); ?>
    </address>
    <?php
}
add_action('woocommerce_thankyou', 'thank_you_add_address');

修改说明

  • 函数接收woocommerce_thankyou钩子自动传递的$order_id参数,这是感谢页获取订单ID的正确方式
  • 添加if ( ! $order )判断,确保订单对象有效时才执行后续代码,避免无效订单触发错误
  • 移除了错误的get_query_var('order-pay')获取逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 13:57:52