1

Whenever I addSuccessMessage/addErrorMessage it sets the cookie? But I am not able to get that code, where that magic happens.

In messages.js it gets that message by using code

$.cookieStorage.get('mage-messages');

In my case, once it set the cookieStorage it doesn't get clear even though I do it my code at the end of the script.

$.cookieStorage.set('mage-messages', '');
  • You have raised a good point. +1 for that – Shoaib Munir Mar 24 '19 at 4:24
2

I have also faced this similar issue and I got this resolved by implementing complex messages, like this:

Also you can include any custom html in any message also with this technique.

Implementing complex messages:

  1. in modules di.xml:

    enter image description here

  2. Trigger complex message insteed of simple message:

    $this->_messageManager->addComplexSuccessMessage('custom_rfq_success');

  3. create .phtml containing message content: at (/Namespace/Module/view/frontend/templates/messages/custom_rfq_success.phtml)

Just notice in the above of .phtml file having message content apply this:

<script type="text/javascript">
 require(['jquery','domReady!'], function($){
 setTimeout(function()
    {
        $.cookieStorage.set('mage-messages', '');
    }, 1000);
})
</script>

I was able to resolve that sticky message issue by this. Please check if it works well for you.

More information on complex messages: https://store.fooman.co.nz/blog/displaying-complex-error-messages-in-magento-2.html

Thanks.

  • I need to put that each module di.xml? that's a pain – summu Mar 24 '19 at 15:41
  • Can i write the message.phtml content in my custom_rfq_success.phtml, as i need same format, but message.phtml has data-bind and ko stuff (knockout) – summu Mar 24 '19 at 15:51
  • Great solution, with above example an empty message box still shows as you have just a script in the message. I luckily needed a message however, would like to know a way to hide the message box if just wanna run a script? – harri Jan 16 at 8:43
0

Success and error messages stores in customer data, you can always get and set customer data

require(['Magento_Customer/js/customer-data'], function(customerData){
    customerData.set('messages', {messages: ""});
});

This will empty data of messages in customer data.

  • then why in message.js message get and set from mage-messages cookie , in my case it shows all previous cookie messages even though I clear it using $cookieStorage('mage-messages',"") , for iPhone safari it's totally not working – summu Mar 24 '19 at 4:17
  • Magento not just save messages in cookie, it also saves in local storage of your browser, check local storage and you will see, by using the method that I have mentioned, you will able to empty your local storage messages – Shoaib Munir Mar 24 '19 at 4:23
  • In message.phtml also it gets data from cookieMessages <div data-bind="foreach: { data: cookieMessages, as: 'message' }" class="messages">, which we get from cookieStorage , i dont see any role of customerData here – summu Mar 24 '19 at 4:26
  • Check removeAll function in messages.js you will understand – Shoaib Munir Mar 24 '19 at 4:32
  • look for customerData in entire messages.js it will surely clear stuff about customerData – Shoaib Munir Mar 24 '19 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.