Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

}

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.

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!

like image 891
Mary James Avatar asked Jan 06 '16 23:01

Mary James


People also ask

How do I turn off WooCommerce messages?

You can do that with this snippet: add_filter( 'wc_add_to_cart_message_html', '__return_false' ); Add it to the theme's functions. php file or you can use the Code Snippets plugin.

How do I edit WooCommerce notices?

Set new Cart Notices by going to WooCommerce > Cart Notices, where you will see a list of your current Cart Notices and can add, view, edit, enable/disable, and delete your notices.


2 Answers

this worked for me:

add_filter( 'wc_add_to_cart_message', 'remove_add_to_cart_message' );

function remove_add_to_cart_message() {
    return;
}
like image 145
bellmountain Avatar answered Nov 15 '22 06:11

bellmountain


Update: 18/05/2018 Please refer to bellmountain's much simpler answer for the correct way to do this.

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

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

like image 20
James Jones Avatar answered Nov 15 '22 08:11

James Jones