删除WooCommerce中的"Checkout is not available whilst your cart is empty."通知



我应该在functions.php中添加什么代码来删除"结帐不可用,而您的购物车是空的。"Woocommerce.

我在include/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;
}

覆盖核心文件不是一个选项,有什么建议吗?

您可以使用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 );

相关内容

最新更新