9

I custom add to cart module on Magento 2 and keep message like Magento core.

if (!$this->_checkoutSession->getNoCartRedirect(true)) {
    if (!$this->cart->getQuote()->getHasError()) {
        $message = __('You added %1 to your shopping cart.', $product->getName());
        $this->messageManager->addSuccessMessage($message);
    }
    return $this->goBack(null, $product);
}

After add to cart success, it return message "Product name add to cart success". But when I access to another page, that message still display.

Ex: I clicked add to cart, when it was loading, I quickly clicked to cart(checkout/cart). Success message was display at there. How can I remove that message, I just want it display one time at product(where click add to cart) page. I use Magento 2.0.7

4

3 Answers 3

7

[Update]

After all, I found a solution for this problem. It's made me sweat too much.

Message was inited at messages.phtml on Magento_Theme package(module-theme\view\frontend\templates\messages.phtml).

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<div data-bind="scope: 'messages'">
    <div data-bind="foreach: { data: messages().messages, as: 'message' },attr:{class:((messages().messages && messages().messages.length > 0)?'messages block __caution mB30':'')}" class="">
    <p class="caution_text" data-bind="html: message.text, visible:message.text!='One or more input exceptions have occurred.'"></p>
</div>

So I think we have to remove message after it display. So I override magento theme and add this script end of messages.phtm file. It works like a charm.

<script>
    require(['jquery', 'jquery/jquery-storageapi'], function($) {
        var storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
        storage.remove('messages');
    });
</script>

Hope this help!

8
  • Which version of Magento are you using? Commented Jan 24, 2017 at 6:18
  • I add tag 2.0.7 above
    – Henry Bui
    Commented Jan 24, 2017 at 6:21
  • I am using 2.1.3 and it has the same problem Commented Jan 24, 2017 at 6:22
  • Was my answer help you fix it?
    – Henry Bui
    Commented Jan 24, 2017 at 9:08
  • no it doesn't work on my version of Magento Commented Jan 25, 2017 at 5:44
2

For remove message in Magento 2 try this in phtml or js file:

<script>
require(['jquery', 'jquery/jquery-storageapi'], function($) {
    $.cookieStorage.set('mage-messages', '');
});
</script>
1

Actually messages are taken from the messages container and put into a cookie named "mage-messages" here: \Magento\Theme\Controller\Result\MessagePlugin

So I was able to rework this cookie by resetting it with my custom message.

Your business logic may differ if you want to merge your message with the existing one. Just fine tune $messages to your need.

In my case, I needed to clean all messages and replace them with a custom error message. See below:

$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
$publicCookieMetadata->setDurationOneYear();
$publicCookieMetadata->setPath('/');
$publicCookieMetadata->setHttpOnly(false);
$publicCookieMetadata->setSameSite('Strict');

$messages = [
    [
        'type' => 'error',
        'text' => __('Product cannot be added to the wishlist yet. Please log in or create an account in order to be able to add items to your wishlist.'),
    ],
];

$this->cookieManager->setPublicCookie(
    MessagePlugin::MESSAGES_COOKIES_NAME,
    json_encode($messages),
    $publicCookieMetadata
);

Magento EE 2.4.2

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.