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

如何为WooCommerce特定拍卖产品添加18.6%额外手续费

仅针对WooCommerce拍卖产品添加百分比手续费的解决方案

需求说明

仅对店铺中**auction(拍卖产品)**的购物车小计收取18.6%的额外手续费,simple和variant产品不参与该手续费计算。

合并后的完整代码

/** 针对拍卖产品添加额外手续费 */
add_action('woocommerce_cart_calculate_fees', function() {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    $percentage = 0.186;
    $auction_subtotal = 0;

    // 遍历购物车中的所有商品
    foreach (WC()->cart->get_cart() as $cart_item) {
        $product = $cart_item['data'];
        // 判断是否为拍卖产品
        if (method_exists($product, 'get_type') && $product->get_type() == 'auction') {
            // 累加拍卖产品的小计金额(单价×数量)
            $auction_subtotal += $cart_item['line_subtotal'];
        }
    }

    // 如果有拍卖产品,计算并添加手续费
    if ($auction_subtotal > 0) {
        $percentage_fee = $auction_subtotal * $percentage;
        WC()->cart->add_fee(__('[:en]Extra fee[:el]Προμήθεια[:]', 'txtdomain'), $percentage_fee);
    }
});

代码说明

  • 保留后台判断逻辑,避免在后台非AJAX场景下执行代码
  • 遍历购物车所有商品,通过get_type()方法精准识别拍卖产品
  • 累加所有拍卖产品的单条小计金额(已包含商品数量计算)
  • 仅当存在拍卖产品时,计算18.6%的手续费并添加至购物车费用项

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 06:12:36