自定义WooCommerce bKash支付网关PHP代码致命错误排查求助
自定义WooCommerce bKash支付网关插件报错修复方案
你的插件触发严重错误的核心原因是回调函数名不匹配,同时代码存在功能缺失问题,具体修复步骤如下:
1. 致命错误修复:函数名与钩子回调不匹配
在add_action中你写的回调是custom_bkash-payment_init,但实际定义的函数是custom_bkash_payment_init——PHP函数名不允许包含减号,这会直接触发语法错误导致网站崩溃。
修改这一行代码:
add_action( 'plugins_loaded', 'custom_bkash_payment_init', 11);
2. 功能完善:注册支付网关到WooCommerce
即使修复了函数名,你的插件也不会在WooCommerce支付设置中显示,因为缺少网关注册步骤。需要在custom_bkash_payment_init函数末尾添加注册代码:
// 注册支付网关 add_filter( 'woocommerce_payment_gateways', 'add_custom_bkash_payment_gateway' ); function add_custom_bkash_payment_gateway( $gateways ) { $gateways[] = 'WC_Custom_Bkash_Pay_Gateway'; return $gateways; }
3. 完善设置字段
你的enabled字段缺少必要的类型和默认值,WooCommerce无法正确渲染设置界面,修改init_form_fields方法:
public function init_form_fields() { $this->form_fields = apply_filters( 'woo_custom_bkash_pay_fields', array( 'enabled' => array( 'title' => __( '启用/禁用', 'custom-bkash-payment'), 'type' => 'checkbox', 'label' => __( '启用bKash支付', 'custom-bkash-payment'), 'default' => 'no', 'description' => __( '勾选以启用bKash支付方式', 'custom-bkash-payment'), ), )); }
4. 添加设置保存逻辑
需要在构造函数中添加保存设置的动作,确保后台修改的设置能生效:
public function __construct() { $this->id = 'custom_payment'; $this->icon = apply_filters( 'woocommerce_custom_bkash_payment_icon', plugins_url( '/assets/icon.png' , __FILE__ )); $this->has_fields = false; $this->method_title = __( 'bKash支付', 'custom-bkash-payment'); $this->method_description = __( 'bKash支付系统', 'custom-bkash-payment'); $this->init_form_fields(); $this->init_settings(); // 绑定设置保存动作 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); }
完整修复后的代码
<?php /** * Plugin Name: Custom Bkash Payment for WC * Plugin URI: * Description: This plugin helps you to add Bkash store payment link to checkout page. * Version: 1.0.0 * Author: Classified * Author URI: https://example.com * License: GPL-2.0 or later * License URI: http://www.gnu.org/licenses/gpl-2.0.html * Text Domain: custom-bkash-payment */ // 检查WooCommerce是否激活 if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters('active_plugins', get_option( 'active_plugins' )))) return; add_action( 'plugins_loaded', 'custom_bkash_payment_init', 11); function custom_bkash_payment_init() { if ( class_exists('WC_Payment_Gateway')) { class WC_Custom_Bkash_Pay_Gateway extends WC_Payment_Gateway { public function __construct() { $this->id = 'custom_payment'; $this->icon = apply_filters( 'woocommerce_custom_bkash_payment_icon', plugins_url( '/assets/icon.png' , __FILE__ )); $this->has_fields = false; $this->method_title = __( 'bKash支付', 'custom-bkash-payment'); $this->method_description = __( 'bKash支付系统', 'custom-bkash-payment'); $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_custom_bkash_pay_fields', array( 'enabled' => array( 'title' => __( '启用/禁用', 'custom-bkash-payment'), 'type' => 'checkbox', 'label' => __( '启用bKash支付', 'custom-bkash-payment'), 'default' => 'no', 'description' => __( '勾选以启用bKash支付方式', 'custom-bkash-payment'), ), )); } } // 注册支付网关 add_filter( 'woocommerce_payment_gateways', 'add_custom_bkash_payment_gateway' ); function add_custom_bkash_payment_gateway( $gateways ) { $gateways[] = 'WC_Custom_Bkash_Pay_Gateway'; return $gateways; } } }
后续你还需要添加process_payment方法来处理实际的支付流程,否则用户选择该支付方式后无法完成下单。
内容的提问来源于stack exchange,提问作者Sk Hasan Al Sahib
相关产品推荐
相关产品推荐

