I want to display success message on click of add to cart button in magento 2.

I am developing custom module in which i can develop functionality of add to cart and i want to add success and error messages on click of add to cart in Magento 2.

so anyone can have idea then please share with me.

Thank you in Advance.

1 Answer 1


Messages can be displayed by using the MessageManager.


namespace Vendor\Module\Controller\Cart;

class Cart extends \Magento\Framework\App\Action\Action
    protected $_messageManager;
    public function __construct
       \Magento\Framework\App\Action\Context $context,
       \Magento\Framework\Message\ManagerInterface $messageManager
       $this->_messageManager = $messageManager;
    public function execute()
        $this->_messageManager->addSuccessMessage(__('Item Added to Cart Successfully.'));
        $resultRedirect = $this->resultRedirectFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('checkout/cart');

You can use your custom module to overwrite the add to cart action/template and display a message with MessageManager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.