WooCommerce自定义支付网关未在结账页显示的问题求助
WooCommerce支付网关后台启用但结账页不显示的解决方法
问题情况
我正在使用一款名为Noob Payment for Woocommerce的自定义WooCommerce支付网关插件,已经完成后台所需的信息配置,并且在WooCommerce设置-支付页面中启用了该支付方式,但它并未在结账页面显示。同时测试另一款第三方支付插件也出现了相同的问题。我的WordPress版本为4.6.2,自定义插件的代码如下:
<?php /** * Plugin Name: Noob Payment for Woocommerce * Plugin URI: https://omukiguy.com * Author Name: Laurence Bahiirwa * Author URI: https://omukiguy.com * Description: This plugin allows for local content payment systems. * Version: 0.1.0 * License: 0.1.0 * License URL: http://www.gnu.org/licenses/gpl-2.0.txt * text-domain: noob-pay-woo */ if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return; add_action( 'plugins_loaded', 'noob_payment_init', 11 ); function noob_payment_init() { if( class_exists( 'WC_Payment_Gateway' ) ) { class WC_Noob_pay_Gateway extends WC_Payment_Gateway { public function __construct() { $this->id = 'noob_payment'; $this->icon = apply_filters( 'woocommerce_noob_icon', plugins_url('/assets/icon.png', __FILE__ ) ); $this->has_fields = false; $this->method_title = __( 'Noob Payment', 'noob-pay-woo'); $this->method_description = __( 'Noob local content payment systems.', 'noob-pay-woo'); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->instructions = $this->get_option( 'instructions', $this->description ); $this->init_form_fields(); $this->init_settings(); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); } public function init_form_fields() { $this->form_fields = apply_filters( 'woo_noob_pay_fields', array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'noob-pay-woo'), 'type' => 'checkbox', 'label' => __( 'Enable or Disable Noob Payments', 'noob-pay-woo'), 'default' => 'no' ), 'title' => array( 'title' => __( 'Noob Payments Gateway', 'noob-pay-woo'), 'type' => 'text', 'default' => __( 'Noob Payments Gateway', 'noob-pay-woo'), 'desc_tip' => true, 'description' => __( 'Add a new title for the Noob Payments Gateway that customers will see when they are in the checkout page.', 'noob-pay-woo') ), 'description' => array( 'title' => __( 'Noob Payments Gateway Description', 'noob-pay-woo'), 'type' => 'textarea', 'default' => __( 'Please remit your payment to the shop to allow for the delivery to be made', 'noob-pay-woo'), 'desc_tip' => true, 'description' => __( 'Add a new title for the Noob Payments Gateway that customers will see when they are in the checkout page.', 'noob-pay-woo') ), 'instructions' => array( 'title' => __( 'Instructions', 'noob-pay-woo'), 'type' => 'textarea', 'default' => __( 'Default instructions', 'noob-pay-woo'), 'desc_tip' => true, 'description' => __( 'Instructions that will be added to the thank you page and order email', 'noob-pay-woo') ), )); } public function process_payments( $order_id ) { } } } } add_filter( 'woocommerce_payment_gateways', 'add_to_woo_noob_payment_gateway'); function add_to_woo_noob_payment_gateway( $gateways ) { $gateways[] = 'WC_Noob_pay_Gateway'; return $gateways; }
解决方法
1. 修复自定义插件的核心方法错误
你的自定义插件里有个致命错误:支付处理方法名写错了。WooCommerce要求支付网关类必须实现**process_payment**(单数)方法,而你的代码里写的是process_payments(复数)。这个方法是WooCommerce识别网关有效性的关键,名称错误会直接导致网关不在结账页显示。
修正后的方法代码示例:
public function process_payment( $order_id ) { // 获取订单对象 $order = wc_get_order( $order_id ); // 根据业务需求更新订单状态,这里标记为处理中 $order->update_status( 'processing', __( '等待Noob Payment确认', 'noob-pay-woo' ) ); // 清空购物车 WC()->cart->empty_cart(); // 返回跳转至订单感谢页面 return array( 'result' => 'success', 'redirect' => $this->get_return_url( $order ) ); }
2. 排查全局通用问题
因为两款插件都出现相同问题,还要检查以下几点:
- 版本兼容性:WordPress4.6.2是旧版本,确认所用WooCommerce版本是否与WP4.6.2兼容,过新的WooCommerce可能不再支持旧版WP,导致网关加载失败。
- 添加可用性检查方法:有些网关会默认根据订单条件判断是否显示,可在自定义网关类中添加
is_available()方法强制显示,调试后再调整逻辑:
public function is_available() { // 只要后台启用就显示,调试完成后可添加金额、区域等判断条件 return $this->get_option( 'enabled' ) === 'yes'; }
- 调整插件加载顺序:当前代码用
plugins_loaded钩子优先级11加载网关,可尝试提高优先级(比如改为20),确保WooCommerce完全初始化后再加载网关类:
add_action( 'plugins_loaded', 'noob_payment_init', 20 );
- 排查冲突:暂时禁用其他第三方插件,清除网站缓存,测试是否是插件冲突导致网关不显示。
内容的提问来源于stack exchange,提问作者Eslam Kalash
相关产品推荐
相关产品推荐

