3

I want to modify/update the success message added by cart controller (Magento\Checkout\Controller\Cart\Add).

As described here and here, I can use preference to override controller to update success message but I think overriding core classes is bad idea.

So, is there any other better way to update success message instead of preference?

  • You could use translation files to change message. Place the changed message in language translation file under i18n directory. – Chandresh P. Jan 21 '17 at 17:02
  • thanks for solution but it won't work in a case when I want to add some dynamic information with success message.. For example, URL of cart page.. – Shyam Jan 21 '17 at 18:19
  • In case you want to add dynamic information also you need to use plugin/preference only according to me. – Chandresh P. Jan 23 '17 at 7:35
4

We can use plugin or event/observer to update the success message. In my case, I used postdispatch event.

app/code/{Vendor}/{Module}/etc/frontend/events.xml

<event name="controller_action_postdispatch_checkout_cart_add">
    <observer name="updatemessage" instance="{Vendor}\{Module}\Observer\UpdateMessage" />
</event>

In observer, Use \Magento\Framework\Message\ManagerInterface to get last added message and add new/updated success message.

app/code/{Vendor}/{Module}/Observer/UpdateMessage.php

<?php
namespace {Vendor}\{Module}\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class UpdateMessage implements ObserverInterface
{
    /** @var \Magento\Framework\Message\ManagerInterface */
    protected $messageManager;

    /** @var \Magento\Framework\UrlInterface */
    protected $url;

    public function __construct(
        \Magento\Framework\Message\ManagerInterface $managerInterface,
        \Magento\Framework\UrlInterface $url
    ) {
        $this->messageManager = $managerInterface;
        $this->url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $messageCollection = $this->messageManager->getMessages(true);
        $cartLink = '<a href="'. $this->url->getUrl('checkout/cart') .'">View Cart/Checkout</a>';
        $this->messageManager->addSuccess($messageCollection->getLastAddedMessage()->getText() . '  ' . $cartLink);
    }
}
  • Thanks a lot.. I was searching for solution via plugin, but observer is also working fine.. – Mohit Kumar Arora Nov 16 '17 at 5:28
  • @Shyam, is that possible to do this using plugins instead of observer ? – Camit1dk Jun 6 '18 at 6:10
  • @Camit1dk Yes, it is possible. You can use afterExecute() to do the same – Shyam Jun 6 '18 at 6:59
  • Thanks Shyam , What can i used in return value while using plugin ? – Camit1dk Jun 6 '18 at 7:28
  • @Shyam, can you please answer magento.stackexchange.com/questions/246296/…, since you have mentioned that it is possible to do the same using plugin. – Mohit Kumar Arora Nov 6 '18 at 5:34
1

You can change the Add to cart message from the following template file.

vendor/magento/module-checkout/view/frontend/templates/message/addCartSuccessMessage.phtml

Note: Do not make any changes in core files. Override this template file in your theme and make changes there.

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.