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
相关产品推荐
相关产品推荐

