'WooCommerce - unset "<product> removed notice…" on cart page
actions and filters. On my WooCommerce site I get the following message when I remove a product from the shopping cart:
"<product name>" removed. Undo?
Looking over WooCommerce source code I have found a conditional statement in class-wc-form-header.php
as part of the function update_cart_action()
:
$removed_notice .= ' <a href="' . esc_url( WC()->cart->get_undo_url( $cart_item_key ) ) . '">' . __( 'Undo?', 'woocommerce' ) . '</a>';
But I can't find the way to use it for eliminate this notice. I have try css solutions but it didn't work:
PS: that may not be the code snippet that is bothering me, but its the only one I have found that seems to make sense.
How can I remove this bothering notice?
Thanks.
Solution 1:[1]
You can do that in different ways:
1. Overriding the notices.php template:
You have first (if not done yet) to copy woocommerce templates
folder inside your active child theme or theme, then rename it woocommerce
. Then open/edit notices/notices.php
and try to replace the code:
<?php
/**
* Show messages
* ... Blabla ... / ... blabla ...
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! $messages ){
return;
}
?>
<?php foreach ( $messages as $message ) : // Change your template code from here
if ( strpos( $message, 'removed' ) === false ) : ?>
<div class="woocommerce-info"><?php echo wp_kses_post( $message ); ?></div>
<?php endif;
endforeach; ?>
2. Using hooks:
function remove_added_to_cart_notice()
{
$notices = WC()->session->get('wc_notices', array());
foreach( $notices['notices'] as $key => &$notice){
if( strpos( $notice, 'removed' ) !== false){
$added_to_cart_key = $key;
break;
}
}
unset( $notices['notices'][$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);
3. Using CSS (with something like):
.woocommerce-cart .woocommerce-message {
display: none !important;
}
References:
Solution 2:[2]
I had to change this to get it to work. Specifically, the array field is singular notice or at least it is now.
$notices = WC()->session->get('wc_notices', array());
foreach( $notices['notice'] as $key => &$notice){
if( strpos( $notice, 'whilst' ) !== false){
$BadNotice_key = $key;
unset( $notices['notice'][$BadNotice_key] );
WC()->session->set('wc_notices', $notices);
break;
}
}
Solution 3:[3]
Just an update regarding Overriding the notices.php template:
<?php foreach ( $notices as $notice ) :
if ( strpos( $notice['notice'], 'removed' ) === false ) : ?>
<div class="woocommerce-message"<?php echo wc_get_notice_data_attr( $notice ); ?> role="alert">
<?php echo wc_kses_notice( $notice['notice'] ); ?>
</div>
<?php endif;
endforeach; ?>
Had to have add the 'notice' array key to the strpos method or it wasn't finding the "removed" string within the notice message. Hope this helps others who were having issues attempting to use this template override method.
Solution 4:[4]
There is a very simple answer to this problem as there is a hook that you can plug onto.
// This line is to be added in the functions.php
add_filter('woocommerce_cart_item_removed_notice_type', '__return_null');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | Robin B |
Solution 3 | ouija |
Solution 4 | Buzut |