wordpresswoocommercehook-woocommercecheckoutnotice

Remove "Checkout is not available whilst your cart is empty." notice in WooCommerce


What code should I add to functions.php to remove "Checkout is not available whilst your cart is empty." notice in Woocommerce.

I found the code in includes/wc-template-functions.php that is responsible for displaying this message.

// When on the checkout with an empty cart, redirect to cart page.
if ( is_page( wc_get_page_id( 'checkout' ) ) && wc_get_page_id( 'checkout' ) !== wc_get_page_id( 'cart' ) && WC()->cart->is_empty() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) {
    wc_add_notice( __( 'Checkout is not available whilst your cart is empty.', 'woocommerce' ), 'notice' );
    wp_safe_redirect( wc_get_cart_url() );
    exit;

}

Overwriting the core file is NOT an option, any advice?


Solution

  • You can use the woocommerce_checkout_redirect_empty_cart filter hook. Since the message is only displayed if this condition is true

    add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
    

    Another option is to use the woocommerce_add_notice filter hook and if the message matches, return false

    function filter_woocommerce_add_notice ( $message ) {
        // Equal to (Must be exactly the same).
        // If the message is displayed in another language, adjust where necessary!
        if ( $message == 'Checkout is not available whilst your cart is empty.' ) {
            return false;
        }   
        
        return $message;
    }
    add_filter( 'woocommerce_add_notice', 'filter_woocommerce_add_notice', 10, 1 );