How to add link in Success message?

All we need is an easy explanation of the problem, so here it is.

I want to add link in cart added message

Make shopping Cart as link

In checkout cart add controller i am changing message like this

$message = __(
                    'You added %1 to your <a href ="..">shopping cart</a>.',
                    $product->getName()
                );

but its getting text when i look in console its come from knockout js and they are getting html "html: message.text" of message but its getting like text

How can i change it

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.'" rel="nofollow noreferrer noopener">View cart</a>');
    }

    return $this->goBack();
}

Method 2

etc\frontend\di.xml

<type name="Magento\Framework\View\Element\Message\MessageConfigurationsPool">
    <arguments>
        <argument name="configurationsMap" xsi:type="array">
            <item name="addProductAddSuccessMessage" xsi:type="array">
                <item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
                <item name="data" xsi:type="array">
                    <item name="template" xsi:type="string">[Namespace_ModuleName]::messages/addProductSuccessMessage.phtml</item>
                </item>
            </item>
        </argument>
    </arguments>
</type>

Override Magento\Checkout\Controller\Cart\Add

Replace this

 $this->messageManager->addSuccessMessage($message);

with

$this->messageManager->addComplexSuccessMessage(
                    'addProductAddSuccessMessage',
                    [
                        'product_name' => $product->getName(),
                        'referer' => $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl()
                    ]
                );

messages/addProductSuccessMessage.phtml

<?php /* @escapeVerified */ echo __('You added %1 to your ', $block->escapeHtml($block->getData('product_name'))) ?> <?php /* @escapeNotVerified */ echo __('<a href="%1" rel="nofollow noreferrer noopener">shopping cart</a>.', $block->escapeUrl($block->getData('referer')));

Method 3

With the message using in the frontend, it is being processed via function escapeHtml(params $allowedTags = null) in class \Magento\Framework\Escaper, so it is being processed by htmlspecialchars() and we can’t change it.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply