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

求助:WooCommerce订阅 recurring总额无法添加自定义Stripe手续费

Fixing Stripe Fee for WooCommerce Subscriptions Recurring Totals

Got it, let's tackle this problem! Your existing code works for regular carts because it uses the woocommerce_cart_calculate_fees hook, but this hook doesn't trigger when WooCommerce Subscriptions calculates recurring totals for subscription products. Subscriptions have their own dedicated hooks to handle recurring costs, so we need to adjust your function to cover both regular carts and subscription recurring carts.

Here's the Updated Code

function add_stripe_fee_for_cart_and_subscriptions() {
    // Skip admin pages unless we're doing AJAX (checkout uses AJAX for real-time updates)
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }
    
    // Only run on checkout or during subscription recurring fee calculations
    if ( ! is_checkout() && ! did_action( 'woocommerce_subscriptions_recurring_cart_calculate_fees' ) ) {
        return;
    }
    
    // Get the selected payment method (works for both regular and subscription sessions)
    $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
    
    // Exit early if Stripe isn't the selected gateway
    if ( 'stripe' !== $chosen_gateway ) {
        return;
    }
    
    // Check if we're calculating fees for a recurring subscription
    $is_recurring = did_action( 'woocommerce_subscriptions_recurring_cart_calculate_fees' );
    
    // Calculate the base amount: use regular cart total or recurring cart total
    if ( $is_recurring ) {
        $recurring_cart = WC_Subscriptions_Cart::get_recurring_cart();
        $base_amount = $recurring_cart->cart_contents_total;
    } else {
        $base_amount = WC()->cart->cart_contents_total;
    }
    
    // Stripe fee calculation (3.5% + $0.35 - adjust values to match your plan)
    $fee = ( $base_amount * 0.035 ) + 0.35;
    
    // Add the fee to the correct cart object
    if ( $is_recurring ) {
        $recurring_cart->add_fee( 'Stripe Recurring Fee', $fee, false, '' );
    } else {
        WC()->cart->add_fee( 'Stripe Fee', $fee, false, '' );
    }
}

// Hook into regular cart fee calculations
add_action( 'woocommerce_cart_calculate_fees', 'add_stripe_fee_for_cart_and_subscriptions' );

// Hook into subscription recurring cart fee calculations
add_action( 'woocommerce_subscriptions_recurring_cart_calculate_fees', 'add_stripe_fee_for_cart_and_subscriptions' );

// Force checkout update when payment method changes (so fee appears instantly)
function trigger_fee_update_on_payment_switch() {
    if ( is_checkout() ) {
        ?>
        <script type="text/javascript">
            jQuery(document).on('change', 'input[name="payment_method"]', function() {
                jQuery(document.body).trigger('update_checkout');
            });
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'trigger_fee_update_on_payment_switch' );

Key Changes Explained

  • Added Subscription-Specific Hook: We now use woocommerce_subscriptions_recurring_cart_calculate_fees to target the recurring cost calculations for subscription products.
  • Recurring Cart Handling: The code detects when it's calculating for a recurring subscription and uses WC_Subscriptions_Cart::get_recurring_cart() to access the correct cart object instead of the regular shopping cart.
  • AJAX Compatibility: The trigger_fee_update_on_payment_switch function ensures the fee updates immediately when the user selects Stripe (no page reload needed).
  • Cleaner Conditional Checks: We added guards to make sure the function only runs where necessary, avoiding unnecessary processing on admin pages.

Important Notes

  1. Activate WooCommerce Subscriptions: This code depends on the official WooCommerce Subscriptions plugin—make sure it's installed and active on your site.
  2. ** Test with Subscription Products**: Add a subscription product to your cart, select Stripe, and verify the recurring fee shows up in the subscription totals section.
  3. Adjust Fee Values: Modify the 0.035 (3.5%) and 0.35 ($0.35) numbers to match your actual Stripe fee structure.
  4. Taxable Fee Option: If your Stripe fee should be taxable, change the third parameter in add_fee from false to true.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:03:23