删除/隐藏添加到购物车消息的 Woocommerce,但保留/显示优惠券应用的消息


Remove/Hide Woocommerce Added to Cart Message but Keep/Display Coupon Applied Message

我正在尝试删除或隐藏WooCommerce结帐页面顶部的添加到购物车的消息(我已经删除了购物车页面,因此此消息显示在结帐页面上(。我尝试将其添加到我的 CSS 中:

.woocommerce-message {display: none;}. 

虽然这会隐藏我想要的添加到购物车的消息,但它也隐藏了我不想隐藏的优惠券应用消息。

接下来,我在functions.php文件中尝试了Business Bloomer博客中的以下代码片段:

// Removes Product Successfully Added to Cart
add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
echo '<style>.woocommerce-message {display: none !important;}</style>';
}

这会隐藏文本,但应用于具有 .woocommerce-message类div 的样式仍然可见,包括背景色、填充等。所以我在页面顶部留下了一个矩形,里面没有文字。

关于如何完全隐藏添加到购物车的消息的.woocommerce-messagediv,而不是优惠券应用消息或任何其他消息的.woocommerce-messag ediv的任何想法将不胜感激!

这对

我有用:

add_filter( 'wc_add_to_cart_message', 'remove_add_to_cart_message' );
function remove_add_to_cart_message() {
    return;
}

> 更新日期: 18/05/2018 请参考贝尔山更简单的答案,了解正确的方法。

将此代码添加到主题functions.php文件中。它将仅删除该消息。它应该只在可能发生它的页面上触发。

function remove_added_to_cart_notice()
{
    $notices = WC()->session->get('wc_notices', array());
    foreach( $notices['success'] as $key => &$notice){
        if( strpos( $notice, 'has been added' ) !== false){
            $added_to_cart_key = $key;
            break;
        }
    }
    unset( $notices['success'][$added_to_cart_key] );
    WC()->session->set('wc_notices', $notices);
}
add_action('woocommerce_before_single_product','remove_added_to_cart_notice',1);
add_action('woocommerce_shortcode_before_product_cat_loop','remove_added_to_cart_notice',1);
add_action('woocommerce_before_shop_loop','remove_added_to_cart_notice',1);

不要担心使用您尝试过的 css。

我正在使用这个:

add_filter( 'wc_add_to_cart_message_html', '__return_null' );

这应该可以隐藏添加到购物车消息的产品

add_filter( 'wc_add_to_cart_message', 'remove_cart_message' );
function remove_cart_message() {
    return;
}

刚刚使用了以下内容,它工作正常:

div.woocommerce-message {
    display: none !important;
}

希望这有帮助!