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

WooCommerce设置全局最低消费排除餐饮类目及其子类目的问题咨询

WooCommerce 订单最低金额规则合并实现

以下是合并后的完整代码,可直接替换你原来的两段代码,完美匹配业务需求:

/**
 * WooCommerce 全局最低订单金额+餐饮类目例外规则
 */
add_action( 'woocommerce_checkout_process', 'wc_custom_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_custom_minimum_order_amount' );

function wc_custom_minimum_order_amount() {
    // 配置项
    $minimum_global = 51.50; // 全局最低订单金额
    $catering_slug = 'catering'; // 餐饮类目标识
    $taxonomy = 'product_cat';

    $has_non_catering = false; // 是否有非餐饮类商品
    $cart_total = WC()->cart->total;

    // 获取餐饮类目及所有子类目的ID
    $main_term = get_term_by( 'slug', $catering_slug, $taxonomy );
    $children_ids = get_term_children( $main_term->term_id, $taxonomy );
    $catering_term_ids = array_merge( array($main_term->term_id), $children_ids );

    // 遍历购物车判断商品类目
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( !has_term( $catering_term_ids, $taxonomy, $cart_item['product_id'] ) ) {
            $has_non_catering = true;
            break; // 只要找到一个非餐饮类就可以终止遍历
        }
    }

    // 只有存在非餐饮类商品时,才校验全局最低金额
    if ( $has_non_catering && $cart_total < $minimum_global ) {
        $notice_text = sprintf( '您的订单总金额需至少达到%s(含运费)才可提交,当前订单金额为%s。',
            wc_price( $minimum_global ),
            wc_price( $cart_total )
        );
        if( is_cart() ) {
            wc_print_notice( $notice_text, 'error' );
        } else {
            wc_add_notice( $notice_text, 'error' );
        }
    }

    // 如果你需要保留餐饮类目自身的最低消费门槛,可取消注释以下代码,不需要直接删除即可
    /*
    $catering_subtotal = 0;
    $has_catering = false;
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( has_term( $catering_term_ids, $taxonomy, $cart_item['product_id'] ) ) {
            $has_catering = true;
            $catering_subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
        }
    }
    if ( $has_catering && $catering_subtotal < $minimum_global ) {
        wc_add_notice( sprintf(
            '餐饮类目商品订单金额需至少达到%s才可提交',
            wc_price($minimum_global)
        ), 'error' );
    }
    */
}

逻辑说明

  • 购物车全部是餐饮类目商品时,不受51.50欧元最低金额限制,可直接提交订单
  • 购物车只要包含非餐饮类目商品,就会校验总金额是否达到51.50欧元的最低要求
  • 提示文本已改为中文,如需保留原德语提示,替换对应$notice_text的内容即可
  • 代码预留了餐饮类目单独设置最低门槛的可选逻辑,不需要直接删掉注释部分即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 10:48:00