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

如何隐藏WooCommerce购物车中重复显示的错误通知?

WooCommerce AJAX加购时错误通知重复显示的解决方法

问题背景

  • 店铺基于WooCommerce + Elementor搭建,启用AJAX加入购物车功能
  • 为限制商品需按6的倍数购买,在主题functions.php中添加了两段校验代码
  • 异常现象:每次添加商品到购物车时,错误通知会重复显示,旧通知无法自动清除
  • 曾尝试添加清除通知的代码,但未生效

原有校验代码

// check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $multiples = 6;
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
        wc_add_notice( sprintf( __('Bitte beachten Sie, dass wir Weine nur in 6er oder 12er Kartons versenden können', 'woocommerce'), $multiples ), 'error' );
}

// Limit cart items with a certain shipping class to be purchased in multiple only
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
function woocommerce_check_cart_quantities_for_class() {
    $multiples = 6;
    $class = 'bottle';
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $product = get_product( $values['product_id'] );
        if ( $product->get_shipping_class() == $class ) {
            $total_products += $values['quantity'];
        }
    }
    if ( ( $total_products % $multiples ) > 0 )
    {
        wc_add_notice( sprintf( __('Bitte beachten Sie, dass wir Kernöl nur in 6er oder 12er Kartons versenden können', 'woocommerce'), $multiples ), 'error' );}

}

无效的清除代码

//clear notices on cart update
function clear_notices_on_cart_update() { 
    wc_clear_notices();
}; 

// add the filter 
add_filter( 'woocommerce_update_cart_action_cart_updated', 'clear_notices_on_cart_update', 10, 1 ); 

解决方案

问题核心在于AJAX加购场景下,原有钩子触发时机不匹配,且添加新通知前未针对性清除同类旧通知。以下是两种可行的解决方式:

方案一:在添加新通知前清除对应旧通知

修改原有校验函数,在调用wc_add_notice前,先移除已存在的对应错误通知,避免重复:

// 校验所有商品总数量为6的倍数
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $multiples = 6;
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $total_products += $values['quantity'];
    }

    // 清除已存在的对应酒类错误通知
    $current_notices = WC()->session->get('wc_notices', []);
    if ( isset( $current_notices['error'] ) ) {
        $filtered_notices = array_filter( $current_notices['error'], function( $notice ) {
            return strpos( $notice['notice'], 'Weine nur in 6er oder 12er Kartons versenden können' ) === false;
        } );
        $current_notices['error'] = $filtered_notices;
        WC()->session->set( 'wc_notices', $current_notices );
    }

    if ( ( $total_products % $multiples ) > 0 ) {
        wc_add_notice( sprintf( __('Bitte beachten Sie, dass wir Weine nur in 6er oder 12er Kartons versenden können', 'woocommerce'), $multiples ), 'error' );
    }
}

// 校验特定配送类商品数量为6的倍数
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
function woocommerce_check_cart_quantities_for_class() {
    $multiples = 6;
    $class = 'bottle';
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        // 替换过时的get_product为wc_get_product,并增加存在性判断
        $product = wc_get_product( $values['product_id'] );
        if ( $product && $product->get_shipping_class() === $class ) {
            $total_products += $values['quantity'];
        }
    }

    // 清除已存在的对应 Kernöl 错误通知
    $current_notices = WC()->session->get('wc_notices', []);
    if ( isset( $current_notices['error'] ) ) {
        $filtered_notices = array_filter( $current_notices['error'], function( $notice ) {
            return strpos( $notice['notice'], 'Kernöl nur in 6er oder 12er Kartons versenden können' ) === false;
        } );
        $current_notices['error'] = $filtered_notices;
        WC()->session->set( 'wc_notices', $current_notices );
    }

    if ( ( $total_products % $multiples ) > 0 ) {
        wc_add_notice( sprintf( __('Bitte beachten Sie, dass wir Kernöl nur in 6er oder 12er Kartons versenden können', 'woocommerce'), $multiples ), 'error' );
    }
}

方案二:针对AJAX加购场景添加去重钩子

使用woocommerce_ajax_added_to_cart钩子,在AJAX加购完成后对错误通知进行去重处理:

// AJAX加购完成后对错误通知去重
add_action( 'woocommerce_ajax_added_to_cart', 'clear_duplicate_error_notices' );
function clear_duplicate_error_notices() {
    $notices = WC()->session->get('wc_notices', []);
    if ( isset( $notices['error'] ) ) {
        // 按通知内容去重,保留唯一通知
        $unique_notices = [];
        foreach ( $notices['error'] as $notice ) {
            $unique_notices[$notice['notice']] = $notice;
        }
        $notices['error'] = array_values( $unique_notices );
        WC()->session->set('wc_notices', $notices);
    }
}

补充说明

  1. 原代码中get_product()函数已过时,建议替换为wc_get_product()并增加存在性判断,避免因商品获取失败引发报错
  2. 两种方案可结合使用,覆盖非AJAX和AJAX两种场景,确保通知不会重复显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 17:18:22