Skip to content

messages disappear too fast when updating quantities via an ajax request

I’m trying to update quantities in the shopping cart in Magento 2.3.2 via an ajax request but I’m confused about the error messages.

In my code after the ajax request has completed I execute the following js code which I’d like to present any error messages that will be created by Magento e.g. the request qty is not available.

define([
  'jquery',
  'Magento_Checkout/js/action/get-totals',
  'Magento_Customer/js/customer-data',
  'underscore',
  'Magento_Theme/js/view/messages',
  'loader',
  'Ioweb_Cart/node_modules/jquery-form/dist/jquery.form.min',
  'domReady!',
], function ($, getTotalsAction, customerData, _, messageModel) {

...
...

var cookieMessages = $.cookieStorage.get('mage-messages');
        if (!_.isEmpty(cookieMessages)) {
          customerData.set('messages', {messages: cookieMessages});
          $.cookieStorage.set('mage-messages', '');
        }

The code works fine for the most part, but unfortunately the error message shown will disappear really really fast.

enter image description here

I’ve read the various threads about modifying the function

        onHiddenChange: function (isHidden) {
            var self = this;

            // Hide message block if needed
            if (isHidden) {
                setTimeout(function () {
                    $(self.selector).hide('blind', {}, 500);
                }, 5000);
            }
        }

In file Magento_Ui::js/view/messages.js

However this will change the timings only when the page is actually loaded not in my particular example.

I’ve also tried reloading the sections but to no avail.

            var messages_section = ['messages'];
            //customerData.invalidate(messages_sections);
            customerData.reload(messages_section);

So how do I make the message last longer or what is the proper way to force the messages component to refetch the messages from the storage and redraw itself?

What’s even stranger, is that if I refresh the page without my code, the message stays there forever so it doesn’t even go away after 5 seconds like the function suggests.