求助: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_feesto 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_switchfunction 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
- Activate WooCommerce Subscriptions: This code depends on the official WooCommerce Subscriptions plugin—make sure it's installed and active on your site.
- ** 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.
- Adjust Fee Values: Modify the
0.035(3.5%) and0.35($0.35) numbers to match your actual Stripe fee structure. - Taxable Fee Option: If your Stripe fee should be taxable, change the third parameter in
add_feefromfalsetotrue.
内容的提问来源于stack exchange,提问作者mujuonly
相关产品推荐
相关产品推荐

