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

WooCommerce购物车禁止混合不同市场类别商品问题排查

购物车多类别互斥限制问题及解决方案

我改编代码实现购物车中不同Market类别的商品互斥(同一时间仅能存在一类Market商品),但代码存在偶发失效、重复弹出错误提示的问题。

需求规则

  • 购物车中有Market 1商品时,无法添加Market 2或3的商品
  • 购物车中有Market 2商品时,无法添加Market 1或3的商品
  • 购物车中有Market 3商品时,无法添加Market 1或2的商品

问题代码

//*** Prevent mixture of market dates in same cart ***//
function dont_add_market_to_cart_containing_other_market($validation, $product_id) {

// Set flag false until we find a product in cat market (_1,_2_3...)
    $cart_has_market = false;

// Set $cat_check true if a cart item is in market cat
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $product = $cart_item['data'];

        if ( has_term('market-1', 'product_cat', $product->id) || has_term('market-2', 'product_cat', $product->id) || has_term('market-3', 'product_cat', $product->id) ) {
            $cart_has_market = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_is_market = false;
    if ( has_term('market-1', 'product_cat', $product_id) ||  has_term('market-2', 'product_cat', $product_id) || has_term('market-3', 'product_cat', $product_id) ) {
        $product_is_market = true;
    }

// Return true if cart empty
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains market date 1 and product to be added is not market, display error message and return false.
        if ($cart_has_market && !$product_is_market) {
            wc_add_notice('Sorry, you can only purchase indivifual market dates on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
        // If cart contains a product that is not market and product to be added is market, display error message and return false.
        elseif (!$cart_has_market && $product_is_market) {
            wc_add_notice('Sorry, you can only purchase indivifual market dates on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_market_to_cart_containing_other', 10, 2);

问题现象

购物车已有Market 1商品时,添加Market 2商品会被禁止,但每个商品/日期会重复弹出错误提示:

重复弹出的错误提示


修正代码及说明

原代码核心问题:

  1. 仅判断「购物车有无Market类」与「待加商品是否为Market类」的互斥,未处理不同Market子类间的互斥
  2. wc_add_notice重复调用导致多条提示
  3. 函数定义名与钩子绑定名不一致(定义dont_add_market_to_cart_containing_other_market,绑定dont_add_market_to_cart_containing_other)

修正后代码

// 禁止购物车混合不同Market类别的商品
function restrict_mixed_market_categories_in_cart($validation, $product_id) {
    // 获取购物车中已存在的Market类别
    $existing_market_category = false;
    
    // 遍历购物车确认已有Market类别
    foreach (WC()->cart->get_cart() as $cart_item) {
        $product = $cart_item['data'];
        // 精准匹配商品所属的Market子类
        if ($term = has_term(['market-1', 'market-2', 'market-3'], 'product_cat', $product->get_id(), true)) {
            $existing_market_category = $term->slug;
            break; // 找到目标类别后停止遍历
        }
    }

    // 检查待添加商品的Market类别
    $new_product_market = has_term(['market-1', 'market-2', 'market-3'], 'product_cat', $product_id, true);

    // 场景1:购物车已有Market类别,待加商品属于不同Market子类
    if ($existing_market_category && $new_product_market && $existing_market_category !== $new_product_market->slug) {
        wc_clear_notices(); // 清除重复提示
        wc_add_notice('抱歉,同一购物车只能添加同一日期场次的商品。请先结算当前购物车或清空后再添加其他场次商品。', 'error');
        return false;
    }

    // 场景2:购物车已有Market商品,待加商品非Market类
    if ($existing_market_category && !$new_product_market) {
        wc_clear_notices();
        wc_add_notice('抱歉,Market场次商品需单独购买。请先结算当前购物车或清空后再添加此类商品。', 'error');
        return false;
    }

    // 场景3:购物车已有非Market商品,待加商品是Market类
    if (!$existing_market_category && $new_product_market) {
        $has_non_market = false;
        foreach (WC()->cart->get_cart() as $cart_item) {
            $product = $cart_item['data'];
            if (!has_term(['market-1', 'market-2', 'market-3'], 'product_cat', $product->get_id())) {
                $has_non_market = true;
                break;
            }
        }
        if ($has_non_market) {
            wc_clear_notices();
            wc_add_notice('抱歉,Market场次商品需单独购买。请先结算当前购物车或清空后再添加此类商品。', 'error');
            return false;
        }
    }

    return $validation;
}
add_filter('woocommerce_add_to_cart_validation', 'restrict_mixed_market_categories_in_cart', 10, 2);

关键改进

  • 精准识别购物车中具体的Market子类,实现同类别允许、不同类别禁止的核心需求
  • 新增wc_clear_notices()避免重复弹出错误提示
  • 修正函数名与钩子绑定不一致的问题
  • 优化非Market商品与Market商品的互斥判断逻辑
  • 替换为更清晰的中文提示文本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:13:14