phpwordpresswoocommercehook-woocommercenotice

Remove or hide "wc_add_notice" messages in WooCommerce


I am using this to remove the "Cart is updated" message in WooCommerce:

add_filter( 'woocommerce_add_message', '__return_false');

But the "Shipping costs updated" message is still displayed. How can I remove this message?


Solution

  • In your specific case, in shortcodes/class-wc-shortcode-cart.php we find:

    wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
    

    Which in turn calls the wc_add_notice() function:

    ...
    // Backward compatibility.
    if ( 'success' === $notice_type ) {
        $message = apply_filters( 'woocommerce_add_message', $message );
    }
    
    $message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
    ...
    

    (This code was partly copied from: includes/wc-notice-functions.php)


    So instead of using the woocommerce_add_message filter hook you can use the woocommerce_add_"$notice_type" filter hook.

    $notice_type = the name of the notice type - either error, success (default) or notice.

    In your case, $notice_type corresponds with notice. So then just compare the message, if this is sufficient, we return false:

    function filter_woocommerce_add( $message ) {
        // Equal to (Must be exactly the same).
        // If the message is displayed in another language, adjust where necessary!
        if ( $message == 'Shipping costs updated.' ) {
            return false;
        }   
        
        return $message;
    }
    add_filter( 'woocommerce_add_notice', 'filter_woocommerce_add', 10, 1 );
    // OR - Use the desired filter, depending on the name of the notice type (default = success)
    // add_filter( 'woocommerce_add_error', 'filter_woocommerce_add', 10, 1 );
    // add_filter( 'woocommerce_add_success', 'filter_woocommerce_add', 10, 1 );