You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce自定义储值支付方式重装后结账页面不显示求助

问题排查与修复方案

核心问题点

  1. 重复且无效的网关注册:代码里两次调用woocommerce_payment_gateways过滤器,第一次注册的WC_Store_Credit类不存在,导致WooCommerce加载支付网关时出错,直接隐藏了该支付方式。
  2. 支付支持项缺失:新版WooCommerce要求支付网关至少支持'products'和'refunds'等基础项,仅设置'products'可能不符合显示条件。
  3. 钩子ID不匹配:订单状态更新钩子woocommerce_thankyou_hex_store_credit中的后缀是hex_store_credit,但支付方式ID是store_credit,导致钩子无法触发。
  4. 缺少余额验证:结账流程中没有验证用户储值余额是否足够支付订单,存在逻辑漏洞。

修复后的完整代码

// Registering store credit class
add_filter( 'woocommerce_payment_gateways', 'store_credit_class' );
function store_credit_class( $gateways )
{
    $gateways[] = 'Store_Credit_Payment_Method';
    return $gateways;
}

// Creating the custom store credit payment class
add_action( 'plugins_loaded', 'store_credit_gateway' );
function store_credit_gateway()
{
    if ( class_exists( 'WC_Payment_Gateway' ) ) {
        class Store_Credit_Payment_Method extends WC_Payment_Gateway {

            public $order_status;
            public function __construct() {
                $this->id = 'store_credit'; // payment method id
                $this->icon = ''; // payment method icon
                $this->has_fields = true; 
                $this->method_title = esc_html__( 'Store Credit', 'hex-coupon-for-woocommerce' );
                $this->method_description = esc_html__( 'Take payments via store credit', 'coupon' );

                // 补充必要的支持项
                $this->supports = [
                    'products',
                    'refunds',
                    'default_credit_card_form'
                ];

                $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' );

                // 修正钩子ID,与支付方式ID匹配
                add_action( 'woocommerce_thankyou_' . $this->id, [ $this, 'store_credit_update_order_status' ], 10, 1 );
            }

            // All plugin options
            public function init_form_fields()
            {
                $this->form_fields = [
                    'enabled' => [
                        'title'       => esc_html__( 'Enable/Disable', 'coupon' ),
                        'label'       => esc_html__( 'Enable Hex Store Credit', 'coupon' ),
                        'type'        => 'checkbox',
                        'description' => '',
                        'default'     => 'no'
                    ],
                    'title' => [
                        'title'       => esc_html__( 'Title', 'coupon' ),
                        'type'        => 'text',
                        'description' => esc_html__( 'The user will see this during the checkout process.', 'coupon' ),
                        'default'     => esc_html__( 'Store Credit', 'coupon' ),
                        'desc_tip'    => true,
                    ],
                    'description' => [
                        'title'       => esc_html__( 'Description', 'coupon' ),
                        'type'        => 'textarea',
                        'description' => esc_html__( 'This description will be shown during the checkout process.', 'coupon' ),
                        'default'     => esc_html__( 'Pay with your hex store credit.', 'coupon' ),
                    ],
                ];
            }

            // Checkout payment fields
            public function payment_fields()
            {
                if( $this->description ) {
                    echo wpautop( wp_kses_post( $this->description ) );
                    $total_available_store_credit = StoreCreditPaymentHelpers::getInstance()->show_total_remaining_amount();
                }
                ?>
                <div class="form-row form-row-wide" style="border: 1px solid black; padding: 10px">
                    <label><?php echo esc_html__( 'Total credit available', 'coupon' ); ?></label>
                    <b><?php echo wc_price( $total_available_store_credit ); ?></b>
                </div>
                <div class="clear"></div>
                <?php
            }

            // 新增结账验证:检查余额是否足够
            public function validate_fields() {
                $order_total = WC()->cart->total;
                $available_credit = StoreCreditPaymentHelpers::getInstance()->show_total_remaining_amount();
                
                if ( $available_credit < $order_total ) {
                    wc_add_notice( esc_html__( 'Your store credit is insufficient to cover this order.', 'coupon' ), 'error' );
                    return false;
                }
                return true;
            }

            // Processing the payment method
            public function process_payment( $order_id )
            {
                $order = wc_get_order( $order_id );
                $available_credit = StoreCreditPaymentHelpers::getInstance()->show_total_remaining_amount();

                // 扣除储值余额(需根据你的Helper类逻辑调整)
                StoreCreditPaymentHelpers::getInstance()->deduct_credit( $order->get_total() );

                wc_reduce_stock_levels( $order_id );
                WC()->cart->empty_cart();

                // 更新订单状态为已支付
                $order->update_status( 'processing', esc_html__( 'Paid via store credit', 'coupon' ) );
                
                return array(
                    'result' => 'success',
                    'redirect' => $this->get_return_url( $order )
                );
            }

            public function store_credit_update_order_status( $order_id )
            {
                $order = wc_get_order( $order_id );
                if ( $order && $order->get_payment_method() === $this->id ) {
                    $order->update_status( 'processing' );
                }
            }
        }
    }
}

// 触发支付网关的验证逻辑
add_action( 'woocommerce_checkout_process', 'process_store_credit_payment' );
function process_store_credit_payment()
{
    if( $_POST['payment_method'] != 'store_credit' )
        return;
        
    $gateway = new Store_Credit_Payment_Method();
    $gateway->validate_fields();
}

// Display the custom payment details
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order )
{
    if( $order->get_payment_method() != 'store_credit' )
        return;
        
    // 显示储值支付相关的后台订单信息
    echo '<p><strong>' . esc_html__( 'Payment Method:', 'coupon' ) . '</strong> ' . esc_html__( 'Store Credit', 'coupon' ) . '</p>';
}

// Filter hook to show custom confirmation text about store credit in checkout thankyou page
add_filter( 'woocommerce_thankyou_order_received_text', 'custom_store_credit_thankyou_message', 10, 2 );
function custom_store_credit_thankyou_message( $thankyou_message, $order )
{
    $site_url = get_site_url() . '/my-account/store-credit';

    $payment_method = $order->get_payment_method();

    if ( $payment_method === 'store_credit' ) {
        $thankyou_message .= '<div class="store-credit">' . esc_html__( 'You have used store credit as payment method.', 'coupon' ) . '</div>';
        $thankyou_message .= '<a href="' . esc_url( $site_url ) . '">' . esc_html__( 'Check the store credit logs to see the details', 'coupon' ) . '</a>';
    }

    return $thankyou_message;
}

修复说明

  1. 移除无效注册:删除了第一次注册不存在的WC_Store_Credit类的代码,只保留有效的支付类注册。
  2. 补充支持项:在supports数组中添加了'refunds'和'default_credit_card_form',符合新版WooCommerce的要求。
  3. 修正钩子ID:使用$this->id动态生成钩子后缀,确保与支付方式ID一致。
  4. 添加余额验证:新增validate_fields方法,在结账时检查用户储值余额是否足够,避免支付失败。
  5. 优化支付流程:在process_payment中添加了储值余额扣除逻辑(需根据你的StoreCreditPaymentHelpers类实际方法调整),并直接更新订单状态。
  6. 完善后台显示:在后台订单页面添加了储值支付的标识信息。

内容的提问来源于stack exchange,提问作者Shahadat Hossain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 21:39:52