Hide Added to Cart message in Woocommerce
Update for Woocommerce 3+
The hook wc_add_to_cart_message
is deprecated and replaced by wc_add_to_cart_message_html
. You can use the following (compact effective way):
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
Or the normal way:
add_filter( 'wc_add_to_cart_message_html', 'empty_wc_add_to_cart_message');
function empty_wc_add_to_cart_message( $message, $products ) {
return '';
};
Before Woocommerce 3, use this:
Removing only the message (pasting it to your function.php
file inside your active child theme or theme). This function will return an empty message:
add_filter( 'wc_add_to_cart_message', 'empty_wc_add_to_cart_message', 10, 2 );
function empty_wc_add_to_cart_message( $message, $product_id ) {
return '';
};
Code goes in function.php file of your active child theme (or active theme).
Note: wc_add_to_cart_message
replace deprecated hook woocommerce_add_to_cart_message
.
(UPDATED)
CSS: Removing top message box on checkout page (add this css rule to the style.css
file located inside your active child theme or theme):
.woocommerce-checkout .woocommerce .woocommerce-message {
display:none !important;
}
Remove/Hide Woocommerce Added to Cart Message but Keep/Display Coupon Applied Message
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.
Woocommerce, show/hide added to cart message based on parameters
I managed to find solution:
function filter_wc_add_to_cart_message_html( $message, $products ) {
$urlp = explode('/', $_SERVER['REQUEST_URI']);
$urlp = $urlp[1];
if ( $urlp == 'potwierdzenie'){
return $message;
}
return null;
}
I just check if user in checkout url when buying product, then I'm showing the notice. If user is still on shop page or any other page, notice won't show. This prevents the multiple 'added to cart' messages in checkout.
Related Topics
How to Get the Current PHP Executable from Within a Script
What Does PHP Do with Deprecated Functions
Getting All $_Post from Multiple Select Value
Why Is the Semicolon Optional in the Last Statement in PHP
Installing Mcrypt on Osx 10.8.2 - PHP 5.3.15 with Homebrew
Convert an Array of Strings, Each String Has Dot Separated Values, to a Multidimensional Array
Mongodb Get _Id as String in Find Query
HTML Purifier: Removing an Element Conditionally Based on Its Attributes
Simple PHP Stuff:Variable Evaluation
How to Check Whether Suhosin Is Installed
How to Detect If Your Website Visitor Came from a Google Search Result
Mysql: Determine Table's Primary Key Dynamically
How to Recursively Obtain the "Parent Id" of Rows in This MySQL Table
Are There Different Types of Double Quotes in Utf-8 (Php, Str_Replace)
Insert Value List Does Not Match Column List: 1136 Column Count Doesn't Match Value Count
Switching to Prepared Statements
Is There a Function Similar to Settimeout() (Javascript) for PHP