WordPress自定义支付方式感谢页及邮件内容重复问题解决
自定义WooCommerce支付方式:感谢页/邮件支付指令重复显示问题解决
我基于WordPress为团队开发了一款自定义支付方式,除感谢页功能外其余均正常运行。用户完成订单后,感谢页和订单邮件里的支付指令内容会重复显示,一开始找不到问题根源。
自定义支付方式核心实现代码
public function __construct() { $this->id = 'coupon'; $this->icon = ''; $this->has_fields = true; $this->title = __('Payment Coupon', 'text-domain'); $this->method_description = __('Custom coupon payment gateway', 'woocommerce'); // payment method description // load the settings. $this->init_form_fields(); $this->init_settings(); // Define user set variables. $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); $this->instructions = $this->get_option( 'instructions'); $this->enabled = $this->get_option('enabled'); add_action( 'woocommerce_thankyou_coupon', array( $this, 'thankyou_page' ) ); add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 ); // Action hook to save the settings if (is_admin()) { add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); } } public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => __('Enable/Disable', 'woocommerce'), 'label' => __('Enable Payment Coupon', 'text-domain'), 'type' => 'checkbox', 'description' => __('This enable the Payment Coupon which allow to accept payment through transcash coupon.', 'woocommerce'), 'default' => 'no', 'desc_tip' => true ), 'title' => array( 'title' => __('Title', 'woocommerce'), 'type' => 'safe_text', 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'), 'default' => __('Coupon transcash', 'woocommerce'), 'desc_tip' => true, ), 'description' => array( 'title' => __('Description', 'woocommerce'), 'type' => 'textarea', 'description' => __('Payment method description that the customer will see on your checkout.', 'woocommerce'), 'default' => __("Payer à l'aide d'un coupon transcash.", 'woocommerce'), 'desc_tip' => true, ), 'instructions' => array( 'title' => __('Instructions', 'woocommerce'), 'type' => 'textarea', 'description' => __('Instructions qui seront ajoutés à la page de remerciement et dans les e-mails.', 'woocommerce'), 'default' => '', 'desc_tip' => true, ) ); } /** * Output for the order received page. * * @param int $order_id Order ID. */ public function thankyou_page( $order_id ) { if ( $this->instructions ) { echo wp_kses_post( wpautop( wptexturize( wp_kses_post( $this->instructions ) ) ) ); } }
问题排查过程
- 首先替换
add_action( 'woocommerce_thankyou_coupon', array( $this, 'thankyou_page' ) );为调试代码,查看动作触发次数:
add_action('woocommerce_thankyou_coupon', function() { error_log('Action woocommerce_thankyou_coupon déclenchée depuis : ' . debug_backtrace()[0]['file'] . ' à la ligne ' . debug_backtrace()[0]['line']); });
日志显示该动作被触发两次,但无法直接定位原因。
- 接着在
thankyou_page方法中添加回溯代码,查看调用栈:
public function thankyou_page( $order_id ) { if ( $this->instructions ) { echo wp_debug_backtrace_summary(); echo wp_kses_post( wpautop( wptexturize( wp_kses_post( $this->instructions ) ) ) ); } }
得到重复的调用栈信息后,逐步排查代码,最终发现删除类末尾的add_filter('woocommerce_payment_gateways', 'add_custom_gateway_class');代码后,重复显示的问题完全解决。
内容的提问来源于stack exchange,提问作者Zhalfir
相关产品推荐
相关产品推荐

