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

如何修改WooCommerce附加费代码:全交易收百分比附加费并排除法国

WooCommerce 百分比附加费(排除法国地区)修改方案

以下是修改后的完整代码,可实现所有交易收取百分比附加费且排除法国地区的需求:

/**
 * Add a percentage surcharge to all transactions except France in cart / checkout
 */
add_action( 'woocommerce_cart_calculate_fees','wc_add_percentage_surcharge' ); 
function wc_add_percentage_surcharge() { 
    global $woocommerce; 

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    // 设置附加费百分比(例如2.5%就填2.5)
    $percentage = 2.5;
    // 获取购物车小计金额
    $subtotal = $woocommerce->cart->get_subtotal();
    // 计算附加费金额
    $fee = $subtotal * ($percentage / 100);

    // 排除法国地区(国家代码FR)
    if ( WC()->customer->get_shipping_country() !== 'FR' ) : 
        $woocommerce->cart->add_fee( "服务费 ({$percentage}%)", $fee, true, 'standard' );  
    endif;
}

关键修改说明:

  • 地区判断逻辑调整:原代码仅给美国用户加附加费,现在改为除法国(国家代码FR)外的所有用户都收取附加费
  • 收费模式切换:从固定金额改为按购物车小计的百分比计算,通过get_subtotal()获取基准金额后完成比例换算
  • 名称优化:附加费名称增加百分比提示,让用户直观了解收费比例
  • 兼容原有规则:保留了原代码中含税计算、税类设置的参数,确保和店铺现有税务规则匹配

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 14:41:04