skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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:

     function woo_register_session() {
       if(!session_id() || session_status() == PHP_SESSION_NONE){
           session_start();
       }
       $_SESSION['show_cmsg'] = true;
     }
    add_action( 'init', 'woo_register_session' );
    

    Then get the session and use it in your function accordingly

    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 = $_SESSION['show_cmsg'] ? true : false;
        if ( $showMSG ){
            return $message;
        }
        return null;
    }
    

    Another alternate function for message

    add_filter( 'wc_add_to_cart_message', 'woo_add_to_cart_message' );
    function woo_add_to_cart_message() { 
        $showMSG = $_SESSION['show_cmsg'] ? true : false;
        if($showMSG){
            if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
                $return_to  = get_permalink(woocommerce_get_page_id('shop'));
                $message    = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
            else :
                $message    = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
            endif;
        }else{
            $message = null;
        }
        return $message;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search