How to add link in Success message?
HTML is not allowed by default in flash messages and it's escaped. To change this you can use addSuccess()
method instead of addSuccessMessage()
to create message.
It can be done by creating plugin forMagento\Checkout\Controller\Cart\Add
controller.
I would avoid overwriting whole execute()
method only for this small change and rather use the following:
public function afterExecute()
{
$lastAddedMessage = $this->messageManager->getMessages()->getLastAddedMessage();
if ($lastAddedMessage && $lastAddedMessage->getType() == MessageInterface::TYPE_SUCCESS) {
$text = $lastAddedMessage->getText();
/**
* @var \Magento\Framework\Message\Collection
*/
$messages = $this->messageManager->getMessages();
$messages->deleteMessageByIdentifier($lastAddedMessage->getIdentifier());
$cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl();
$this->messageManager->addSuccess($text . '<a href="'.$cartUrl.'">View cart</a>');
}
return $this->goBack();
}