Woocommerce订单跳转自定义页面及移除按钮问题求助
WooCommerce定制问题解决方案
问题1:订单完成后无法跳转到自定义页面
问题原因
- 未全局声明
$wp变量,导致无法正确识别order-received查询参数 template_redirect钩子未设置优先级,执行时机晚于WooCommerce默认逻辑,无法拦截跳转is_checkout()在订单接收页面的判断不准确
修复代码
提供两种可选方案:
方案1:优化template_redirect钩子实现
public function redirect_customer_after_order() { global $wp; // 直接检测订单接收页面的查询参数 if ( isset( $wp->query_vars['order-received'] ) ) { wp_safe_redirect( site_url( 'riepilogo-ordine' ) ); exit; } } // 设置优先级为1,确保在WooCommerce默认逻辑前执行 add_action( 'template_redirect', array( $this, 'redirect_customer_after_order' ), 1 );
方案2:使用WooCommerce专用钩子woocommerce_thankyou
该钩子专门用于订单感谢页的动作处理,逻辑更精准:
public function redirect_after_thankyou( $order_id ) { wp_safe_redirect( site_url( 'riepilogo-ordine' ) ); exit; } add_action( 'woocommerce_thankyou', array( $this, 'redirect_after_thankyou' ), 1 );
问题2:无法移除“再次下单”按钮
问题原因
- 在
__construct中直接执行remove_action时机过早,WooCommerce尚未加载woocommerce_order_again_button动作 - 未指定与添加动作时一致的优先级(WooCommerce默认用优先级10添加该按钮)
修复代码
public function __construct() { // 延迟到init钩子执行,确保WooCommerce已加载相关动作 add_action( 'init', array( $this, 'remove_order_again_button' ) ); } public function remove_order_again_button() { // 匹配WooCommerce添加动作时的优先级10 remove_action( 'woocommerce_order_details_after_order_table', 'woocommerce_order_again_button', 10 ); }
内容的提问来源于stack exchange,提问作者OHICT
相关产品推荐
相关产品推荐

