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

WooCommerce购物车含A类商品时自动移除B/C/D类商品实现方法

WooCommerce购物车指定商品存在时自动移除对应分类商品实现方案

以下代码可直接插入主题functions.php文件,或通过WooCommerce代码片段插件加载,无需修改核心逻辑,分两个适配版本按需选用即可,触发时机覆盖加购操作、购物车页加载、结算页加载全场景,移除商品时会同步给用户展示前端提示。


版本1:按分类规则触发(匹配核心需求)

逻辑:只要购物车中存在属于分类A的商品,自动移除所有属于分类B、C、D的商品。

add_action( 'woocommerce_before_calculate_totals', 'auto_remove_categories_when_trigger_product_exist', 20, 1 );
function auto_remove_categories_when_trigger_product_exist( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    // 以下参数请自行替换为站点实际值
    $trigger_category_slugs = array('category-a'); // 触发规则的分类A的slug,支持填多个
    $remove_category_slugs = array('category-b','category-c','category-d'); // 需要被移除的分类B/C/D的slug
    $removed_products = array();

    // 第一遍遍历:检查购物车是否存在触发分类的商品
    $has_trigger = false;
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( $trigger_category_slugs, 'product_cat', $cart_item['product_id'] ) ) {
            $has_trigger = true;
            break;
        }
    }

    // 存在触发商品时,移除对应分类商品
    if ( $has_trigger ) {
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( has_term( $remove_category_slugs, 'product_cat', $cart_item['product_id'] ) ) {
                $removed_products[] = $cart_item['data']->get_name();
                $cart->remove_cart_item( $cart_item_key );
            }
        }
        // 展示移除提示
        if ( !empty($removed_products) ) {
            wc_add_notice( sprintf( '因购物车中存在特殊规则商品,已自动移除商品:%s', implode('、', $removed_products) ), 'notice' );
        }
    }
}

版本2:按指定商品ID触发(兼容分类判断异常场景)

如果站点存在自定义分类逻辑导致分类判断失效,可以用这个版本:只要购物车中存在你指定ID的商品A,就自动移除B/C/D分类的商品。

add_action( 'woocommerce_before_calculate_totals', 'auto_remove_categories_when_trigger_id_exist', 20, 1 );
function auto_remove_categories_when_trigger_id_exist( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    // 以下参数请自行替换为站点实际值
    $trigger_product_ids = array(123); // 触发规则的商品A的ID,支持填多个
    $remove_category_slugs = array('category-b','category-c','category-d'); // 需要被移除的分类B/C/D的slug
    $removed_products = array();

    // 第一遍遍历:检查购物车是否存在触发ID的商品
    $has_trigger = false;
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( in_array( $cart_item['product_id'], $trigger_product_ids ) ) {
            $has_trigger = true;
            break;
        }
    }

    // 存在触发商品时,移除对应分类商品
    if ( $has_trigger ) {
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( has_term( $remove_category_slugs, 'product_cat', $cart_item['product_id'] ) ) {
                $removed_products[] = $cart_item['data']->get_name();
                $cart->remove_cart_item( $cart_item_key );
            }
        }
        // 展示移除提示
        if ( !empty($removed_products) ) {
            wc_add_notice( sprintf( '因购物车中存在特殊规则商品,已自动移除商品:%s', implode('、', $removed_products) ), 'notice' );
        }
    }
}

使用注意事项

  • 不要直接填写分类的中文名称作为slug,分类slug可以在WordPress后台「产品-分类」页面,鼠标悬浮到对应分类上,从浏览器底部显示的链接中找到tag=后面的参数值
  • 如果需要多个触发分类/触发商品,直接在对应数组里追加值即可,多个值之间用英文逗号分隔
  • 代码插入后建议清空站点缓存、浏览器缓存后再测试,避免缓存导致规则不生效
  • 如果需要移除的是具体指定商品而非分类商品,只需要把版本2里判断分类的逻辑替换为in_array( $cart_item['product_id'], $需要移除的商品ID数组 )即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 04:51:26