0

I have the same message issue in this thread Magento2 success message However, that post is old and my current version is 2.4.3

Any suggestion for fixing it?

Thanks

2 Answers 2

2

Try This

Method-1 Using Module

app/code/VendoreName/ModuleName/view/frontend

requirejs-config.js

var config = {
    map: {
        '*': {
            'Magento_Theme/js/view/messages':'VendoreName_ModuleName/js/view/messages'
        }
    }
};

app/code/VendoreName/ModuleName/view/frontend/web/js/view

messages.js

define([
    'jquery',
    'uiComponent',
    'Magento_Customer/js/customer-data',
    'underscore',
    'escaper',
    'jquery/jquery-storageapi'
], function ($, Component, customerData, _, escaper) {
    'use strict';

    return Component.extend({
        defaults: {
            cookieMessages: [],
            messages: [],
            allowedTags: ['div', 'span', 'b', 'strong', 'i', 'em', 'u', 'a']
        },

        /**
         * Extends Component object by storage observable messages.
         */
        initialize: function () {
            this._super();

            this.cookieMessages = _.unique($.cookieStorage.get('mage-messages'), 'text');
            this.messages = customerData.get('messages').extend({
                disposableCustomerData: 'messages'
            });

            // Force to clean obsolete messages
            if (!_.isEmpty(this.messages().messages)) {
                customerData.set('messages', {});
            }

            $.cookieStorage.setConf({path: '/', expires: -1}).set('mage-messages', null);

        },

        /**
         * Prepare the given message to be rendered as HTML
         *
         * @param {String} message
         * @return {String}
         */
        prepareMessageForHtml: function (message) {
            return escaper.escapeHtml(message, this.allowedTags);
        }
    });
});

Method-2 Using Theme

app/design/frontend/your-vendor-name/your-theme-name/Magento_Theme/web/js/view

messages.js

define([
    'jquery',
    'uiComponent',
    'Magento_Customer/js/customer-data',
    'underscore',
    'escaper',
    'jquery/jquery-storageapi'
], function ($, Component, customerData, _, escaper) {
    'use strict';

    return Component.extend({
        defaults: {
            cookieMessages: [],
            messages: [],
            allowedTags: ['div', 'span', 'b', 'strong', 'i', 'em', 'u', 'a']
        },

        /**
         * Extends Component object by storage observable messages.
         */
        initialize: function () {
            this._super();

            this.cookieMessages = _.unique($.cookieStorage.get('mage-messages'), 'text');
            this.messages = customerData.get('messages').extend({
                disposableCustomerData: 'messages'
            });

            // Force to clean obsolete messages
            if (!_.isEmpty(this.messages().messages)) {
                customerData.set('messages', {});
            }

            $.cookieStorage.setConf({path: '/', expires: -1}).set('mage-messages', null);

        },

        /**
         * Prepare the given message to be rendered as HTML
         *
         * @param {String} message
         * @return {String}
         */
        prepareMessageForHtml: function (message) {
            return escaper.escapeHtml(message, this.allowedTags);
        }
    });
});

Must be run Magento 2 commands after adding the above files

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento s:d:c
php bin/magento c:c
1

Need to override this file

vendor/magento/module-theme/view/frontend/web/js/view/messages.js

Find the following function

$.mage.cookies.set('mage-messages', '', {
   samesite: 'strict',
   domain: ''
});

Add path on that

$.mage.cookies.set('mage-messages', '', {
   samesite: 'strict',
   domain: '',
   path: '/'
});
1
  • I tried at this path messages.js /app/code/Magento/Theme/view/frontend/web/js/view/messages.js but is not working.. Commented Apr 30, 2022 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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