首頁  >  問答  >  主體

消除 WooCommerce 中聲明「購物車為空時無法結帳」的通知。

我應該在 functions.php 中加入哪些程式碼來刪除「購物車為空時無法結帳」。 Woocommerce 中的通知。

我在includes/wc-template-functions.php中找到了負責顯示此訊息的程式碼。

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

}

覆蓋核心檔案不是一個選項,有什麼建議嗎?

P粉336536706P粉336536706305 天前890

全部回覆(1)我來回復

  • P粉561438407

    P粉5614384072023-11-18 09:39:55

    您可以使用woocommerce_checkout_redirect_empty_cart過濾器掛鉤。由於僅當此條件為真時才會顯示該訊息

    add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
    

    另一種選擇是使用 woocommerce_add_notice 過濾器掛鉤,如果訊息匹配,則傳回 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 );
    

    回覆
    0
  • 取消回覆