When adding product to cart user is redirected to cart/payment window and has ‘added to cart’ notice present. This is the default way I would like to keep, but with addition of my custom method.
My custom button allows customer to stay on shop site while adding another products. However when he enters cart/payment he gets flooded with multiple ‘added to cart’ messages, which looks just bad:
multiple messages
I want to know if there is option to turn off messages but only if user clicked my custom button.
I’ve tried attaching get parameter to button and reading it in my code:
add_filter( 'wc_add_to_cart_message_html', 'filter_wc_add_to_cart_message_html', 10, 2 );
function filter_wc_add_to_cart_message_html( $message, $products ) {
$showMSG = get_query_var('show-cmsg');
if ( $showMSG ){
return $message;
}
return null;
}
But the get_query_var always results in null, no matter the get parameters. I’ve registered it:
function add_custom_query_vars( $vars ){
$vars[] = "show-cmsg";
return $vars;
}
Is there a way to hide the messages depending on which button user pressed?
Or if you can block woocommerce from genereting messages if you pressed custom button?
Or just a way to even limit the messages to 1 most recent instead of showing all at once?
I would also do same for some other error mesasges, but If i would find answer to this then I quess i would manage errors.
2
Answers
I managed to find solution:
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.
You should use session because add_custom_query_vars just add key not value that’s why you getting always null.
So for the session first check session_start() or not, then set the value in the session after that you can get anywhere accordingly.
Example:
Then get the session and use it in your function accordingly
Another alternate function for message