删除/隐藏Woocommerce已添加到购物车消息,但保留/显示优惠券已应用消息 [英] Remove/Hide Woocommerce Added to Cart Message but Keep/Display Coupon Applied Message

查看:95
本文介绍了删除/隐藏Woocommerce已添加到购物车消息,但保留/显示优惠券已应用消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to remove or hide the added to cart message at the top of my WooCommerce checkout page (I have removed the cart page so this message is showing up on the checkout page). I tried adding this to my CSS:

.woocommerce-message {display: none;}. 

尽管这会隐藏添加到购物车中的消息,但它也会隐藏我不想隐藏的已应用优惠券的消息.

Although this hides the added to cart message as I want it to, it also hides the coupon applied message, which I do not want hidden.

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

Next I tried this code snippet from the Business Bloomer blog in the functions.php file:

// 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的样式仍然可见,包括背景色,填充等.因此,我在页面顶部留了一个矩形,没有里面的文字.

This hides the text, but the styles applied to the div with the class of .woocommerce-message are still visible, including background-color, padding etc. So I'm left with a rectangle at the top of my page with no text in it.

任何关于我如何完全隐藏仅添加到购物车消息的.woocommerce-message div的想法,但对于将优惠券应用的消息或任何其他消息的.woocommerce-messag e div完全隐藏的想法,将不胜感激!

Any thoughts on how I can completely hide the .woocommerce-message div just for the added to cart message, but not the .woocommerce-message div for the coupon applied message or any other messages would be appreciated!

推荐答案

更新:18/05/2018有关正确的方法,请参阅bellmountain的简单答案.

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

Add this code to your themes functions.php file. It will remove only that message. It should trigger on just the pages where it is likely to occur.

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.

Don't worry about using that css you've tried.

这篇关于删除/隐藏Woocommerce已添加到购物车消息,但保留/显示优惠券已应用消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆