WooCommerce结账总额中支付方式附加费计算异常求助
WooCommerce支付方式附加费异常:手续费显示但不计入总额,自定义代码失效
我在给客户的电商店铺做WooCommerce支付方式附加费功能,目标是给PayPal和Stripe两种支付方式分别添加手续费,但遇到了两个问题:
- 用插件实现时,不管选哪种支付方式,PayPal的手续费都会被加到所有订单里
- 用自定义代码实现时,手续费完全没生效;更奇怪的是,在结账页面选支付方式时,手续费会显示出来,但不会计入最终订单总额
已经排除了缓存问题:禁用了缓存插件、清除了浏览器历史,还在隐身模式下测试过,问题依旧。
有没有人遇到过类似的问题?求相关解决建议。
我的自定义代码如下:
add_action('woocommerce_cart_calculate_fees', 'add_paypal_fee', 20, 1); function add_paypal_fee($cart) { if (is_admin() && !defined('DOING_AJAX')) return; if (!WC()->session || !WC()->cart) return; $chosen_gateway = WC()->session->get('chosen_payment_method'); $paypal_gateways = [ 'ppcp-gateway', 'ppcp-card-button-gateway', 'ppcp-credit-card-gateway' ]; if (in_array($chosen_gateway, $paypal_gateways, true)) { $percentage = 3.49; // Your PayPal percentage $fixed = 0.49; // Your PayPal fixed fee $subtotal = $cart->get_subtotal(); // Gets cart subtotal $fee = round(($subtotal * $percentage / 100) + $fixed, 2); // Add fee without taxable parameter $cart->add_fee(__('PayPal Processing Fee', 'woocommerce'), $fee, false); } }
内容的提问来源于stack exchange,提问作者MarvWeb
相关产品推荐
相关产品推荐

