How to hide mulitple shown error notice in my cart?

32 Views Asked by At

In my shop https://buschenschank.morbus-schlatter.at/produktliste/ I use woocommerce, elementor and ajax add to cart. I just want to sell my bottles in total of six therefore I used this code in my theme 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' );}

}

Everytime I add a product to the cart it start to show the error message without clearing the old one. You can see the problem on the screenshot enter image description here

I tried

//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 ); 

but didn't work

1

There are 1 best solutions below

6
Vimal Usadadiya On

Kindly use below filter for disable all notice in woocommerce.

add_filter( 'woocommerce_notice_types', '__return_empty_array' );