6

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!

James Jones
  • 3,850
  • 5
  • 25
  • 44
Mary James
  • 256
  • 1
  • 3
  • 14

5 Answers5

20

this worked for me:

add_filter( 'wc_add_to_cart_message', 'remove_add_to_cart_message' );

function remove_add_to_cart_message() {
    return;
}
bellmountain
  • 301
  • 2
  • 3
  • This is valid answer. – Ivijan Stefan Stipić May 17 '18 at 11:57
  • @Ivijan Stefan Stipić This answer certainly supersedes mine but it's not quite correct. `wc_add_to_cart_message` is deprecated and `wc_add_to_cart_message_html` should be used in it's place. – James Jones May 17 '18 at 14:55
  • 4
    We can use `add_filter( 'wc_add_to_cart_message_html', '__return_false' );` – Pari Aug 24 '18 at 13:14
  • Thanks bellmountain and @JamesJones but this seems to remove the added-to-cart notice from all pages. What if I want to remove it only from cart and checkout page? (but leave it on product page?) – J.K. Sep 06 '21 at 12:58
  • @J.K Modify the above function to include some conditionals. Try something like this `function remove_add_to_cart_message($message){ if ( is_cart() || is_checkout() ) { return; } return $message; }` – James Jones Sep 07 '21 at 06:38
5

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.

James Jones
  • 3,850
  • 5
  • 25
  • 44
  • 1
    Thanks for your answer, that got me started in the right direction! I added your suggested code to my site's functions.php file, but it didn't seem to be working. When I added a product to my cart, the message still showed at the top of the checkout page (my product page goes directly to my checkout page, bypassing the shopping cart page). I was able to get it working, however, by using a theme specific hook: add_action('pexeto_before_content','remove_added_to_cart_notice',1);. I am using the Pexeto Story theme. Thanks again! – Mary James Jan 08 '16 at 02:23
  • No worries, glad it could help you! – James Jones Jan 08 '16 at 02:31
  • I am using [Optimizer theme](https://optimizerwp.com/). Where can I find the proper hook? – c0dehunter Nov 05 '16 at 07:52
  • @MaciejKrawczyk - I added "Update: 18/05/2018 Please refer to bellmountain's much simpler answer for the correct way to do this." in bold long before your comment. – James Jones Jun 13 '19 at 05:26
5

I`m using this:

add_filter( 'wc_add_to_cart_message_html', '__return_null' );
kh1
  • 120
  • 1
  • 10
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Alex Riabov Aug 03 '18 at 09:33
  • @ Alex Riabov - this answer has as much context as other predating answers on this page. If you have a question ask it. The answer does not need context or how. How filters work is not the scope of this question - go look it up. – Jon Nov 23 '20 at 15:54
1

This should work to hide the product added to cart message

add_filter( 'wc_add_to_cart_message', 'remove_cart_message' );

function remove_cart_message() {
    return;
}
Deepak
  • 185
  • 2
  • 9
-2

Just used the following and it worked fine:

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

Hope this helps!

andy
  • 9