自定义Liberty Dollar WooCommerce支付网关未显示在结账页求助
自定义Liberty Dollar WooCommerce支付网关不显示在结账页面
我开发了一款自定义Liberty Dollar WooCommerce支付网关插件,激活后在WooCommerce->设置->支付页面可正常显示并接收密钥等配置信息,但保存设置后,该支付网关选项未出现在结账页面。
原插件代码
<?php /* * Plugin Name: Liberty Dollar Payment Gateway * Plugin URI: https://libertydollar.nl * Description: Introducing the Liberty Dollar Payment Gateway for WordPress, the ultimate solution for users looking to accept and make payments using Liberty Dollar balances directly on their WordPress websites. * Author: Liberty Dollar Team * Author URI: https://libertydollar.nl * Version: 0.1.2 */ add_action('plugins_loaded', 'liberty_init_class'); function liberty_init_class() { // Check if WooCommerce is active if ( ! class_exists( 'WooCommerce' ) ) { // If WooCommerce is not active, bail out return; } // Continue if WooCommerce is active add_filter( 'woocommerce_payment_gateways', 'liberty_add_gateway_class' ); function liberty_add_gateway_class( $gateways ) { $gateways[] = 'WC_Liberty_Gateway'; return $gateways; } class WC_Liberty_Gateway extends WC_Payment_Gateway { public $testmode; public $private_key; public $publishable_key; public function __construct() { $this->id = 'libertydollar'; $this->icon = 'https://www.libertydollar.nl/static/media/ldfa-round-logo.76072dece7b030176311.png'; $this->has_fields = false; $this->method_title = 'Liberty Dollar Payment Gateway'; $this->method_description = 'Description of Liberty Dollar payment gateway'; $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); $this->testmode = 'yes' === $this->get_option( 'testmode' ); $this->private_key = $this->testmode ? $this->get_option( 'test_private_key' ) : $this->get_option( 'private_key' ); $this->publishable_key = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' ); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) ); add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'change_payment_complete_order_status' ), 10, 3 ); add_action( 'woocommerce_review_order_before_submit', array( $this, 'payment_fields' ) ); } public function init_form_fields(){ $this->form_fields = array( 'enabled' => array( 'title' => 'Enable/Disable', 'label' => 'Enable Liberty Dollar Payment Gateway', 'type' => 'checkbox', 'description' => '', 'default' => 'no' ), 'title' => array( 'title' => 'Title', 'type' => 'text', 'description' => 'This controls the title which the user sees during checkout.', 'default' => 'Liberty Dollar Payment Gateway', 'desc_tip' => true, ), 'description' => array( 'title' => 'Description', 'type' => 'textarea', 'description' => 'This controls the description which the user sees during checkout.', 'default' => 'Pay with Liberty Dollar Wallet.', ), 'testmode' => array( 'title' => 'Test mode', 'label' => 'Enable Test Mode', 'type' => 'checkbox', 'description' => 'Place the payment gateway in test mode using test API keys.', 'default' => 'yes', 'desc_tip' => true, ), 'test_publishable_key' => array( 'title' => 'Test Publishable Key', 'type' => 'text' ), 'test_private_key' => array( 'title' => 'Test Private Key', 'type' => 'password', ), 'publishable_key' => array( 'title' => 'Live Publishable Key', 'type' => 'text' ), 'private_key' => array( 'title' => 'Live Private Key', 'type' => 'password' ) ); } public function payment_fields() { echo '<div id="custom-payment-form">Pay Via LDFA</div>'; } } }
问题原因及修复方案
1. 缺少核心支付处理方法
WooCommerce要求所有支付网关必须实现process_payment()方法,这是网关在结账页面显示的必要条件。没有这个方法,WooCommerce会判定网关不具备支付处理能力,不会将其加载到结账选项中。
2. 错误的支付字段钩子
你额外注册了woocommerce_review_order_before_submit钩子来调用payment_fields(),但WooCommerce会自动在支付选项区域调用该方法,无需手动挂载钩子,多余的钩子会导致逻辑混乱。
修复后的完整代码
<?php /* * Plugin Name: Liberty Dollar Payment Gateway * Plugin URI: https://libertydollar.nl * Description: Introducing the Liberty Dollar Payment Gateway for WordPress, the ultimate solution for users looking to accept and make payments using Liberty Dollar balances directly on their WordPress websites. * Author: Liberty Dollar Team * Author URI: https://libertydollar.nl * Version: 0.1.2 */ add_action('plugins_loaded', 'liberty_init_class'); function liberty_init_class() { // Check if WooCommerce is active if ( ! class_exists( 'WooCommerce' ) ) { return; } add_filter( 'woocommerce_payment_gateways', 'liberty_add_gateway_class' ); function liberty_add_gateway_class( $gateways ) { $gateways[] = 'WC_Liberty_Gateway'; return $gateways; } class WC_Liberty_Gateway extends WC_Payment_Gateway { public $testmode; public $private_key; public $publishable_key; public function __construct() { $this->id = 'libertydollar'; $this->icon = 'https://www.libertydollar.nl/static/media/ldfa-round-logo.76072dece7b030176311.png'; $this->has_fields = false; $this->method_title = 'Liberty Dollar Payment Gateway'; $this->method_description = 'Description of Liberty Dollar payment gateway'; $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); $this->enabled = $this->get_option( 'enabled' ); $this->testmode = 'yes' === $this->get_option( 'testmode' ); $this->private_key = $this->testmode ? $this->get_option( 'test_private_key' ) : $this->get_option( 'private_key' ); $this->publishable_key = $this->testmode ? $this->get_option( 'test_publishable_key' ) : $this->get_option( 'publishable_key' ); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) ); add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'change_payment_complete_order_status' ), 10, 3 ); } public function init_form_fields(){ $this->form_fields = array( 'enabled' => array( 'title' => 'Enable/Disable', 'label' => 'Enable Liberty Dollar Payment Gateway', 'type' => 'checkbox', 'description' => '', 'default' => 'no' ), 'title' => array( 'title' => 'Title', 'type' => 'text', 'description' => 'This controls the title which the user sees during checkout.', 'default' => 'Liberty Dollar Payment Gateway', 'desc_tip' => true, ), 'description' => array( 'title' => 'Description', 'type' => 'textarea', 'description' => 'This controls the description which the user sees during checkout.', 'default' => 'Pay with Liberty Dollar Wallet.', ), 'testmode' => array( 'title' => 'Test mode', 'label' => 'Enable Test Mode', 'type' => 'checkbox', 'description' => 'Place the payment gateway in test mode using test API keys.', 'default' => 'yes', 'desc_tip' => true, ), 'test_publishable_key' => array( 'title' => 'Test Publishable Key', 'type' => 'text' ), 'test_private_key' => array( 'title' => 'Test Private Key', 'type' => 'password', ), 'publishable_key' => array( 'title' => 'Live Publishable Key', 'type' => 'text' ), 'private_key' => array( 'title' => 'Live Private Key', 'type' => 'password' ) ); } public function payment_fields() { echo '<div id="custom-payment-form">Pay Via LDFA</div>'; } // 新增核心支付处理方法 public function process_payment( $order_id ) { $order = wc_get_order( $order_id ); // 标记订单为处理中(可根据实际支付流程调整状态) $order->update_status( 'processing', 'Awaiting Liberty Dollar payment confirmation' ); // 清空购物车 WC()->cart->empty_cart(); // 返回跳转链接到订单感谢页 return array( 'result' => 'success', 'redirect' => $this->get_return_url( $order ) ); } // 可选:实现感谢页逻辑 public function thankyou_page() { if ( $this->description ) { echo wpautop( wptexturize( $this->description ) ); } } // 可选:修改支付完成后的订单状态 public function change_payment_complete_order_status( $status, $order_id, $order ) { return 'completed'; } } }
额外注意事项
- 确保在WooCommerce设置的支付页面中,已将该网关设置为启用状态
process_payment()方法中的逻辑可根据实际的Liberty Dollar支付流程进行调整,比如添加API调用验证支付等操作
内容的提问来源于stack exchange,提问作者PhpXpert International
相关产品推荐
相关产品推荐

