Magento 2 Add Anchor link to Success or Error Message.

You can add anchor tag link in success message, an error message or any notification message in Magento 2 by addSuccessMessage( ), addErrorMessage( ) and addNoticeMessage( ) respectively.

Let’s add a link to the Shopping cart of a product successfully added to the cart.

$shoppingCartUrl = $block->getUrl(checkout/cart);
$this->messageManager->addSuccessMessage(
__('You added %1 to your <a href="%2">shopping cart</a>.',
    $product->getName(),
    $shoppingCartUrl
  )
);

You can add your message under the addSuccessMessage( ) function.

If you want to add a custom link in the error message you need to use addErrorMessage( ) function to display the link.

link in custom message
add custom link in a success message

Always Use,

  • addSuccessMessage() instead of addSuccess()
  • addErrorMessage() instead of addError()
  • addNoticeMessage() instead of addNotice()

addSuccess(), addError(), addNotice() methods are deprecated.