消除 WooCommerce 中声明“购物车为空时无法结账”的通知。
P粉336536706
P粉336536706 2023-11-17 11:41:02
[PHP讨论组]

我应该在 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粉336536706
P粉336536706

全部回复(1)
P粉561438407

您可以使用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 );
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号