I want to remove the Success message when adding to cart. Right now when you click on the Add to Cart button it displays a message of Successfully added <product> to cart but I don't want to display this. Is there a way to achieve this?
Remove added to cart message Magento 2
4.2k views Asked by MadzQuestioning At
3
There are 3 answers
0
On
You have to add afterExecute plugin to Magento\Checkout\Controller\Cart\Add class:
class DisableAddToCartMessage
{
/** @var string */
protected const DEFAULT_MESSAGE_IDENTIFIER = 'default_message_identifier';
/** @var string */
protected const ADD_TO_CART_SUCCESS_MESSAGE_IDENTIFIER = 'addCartSuccessMessage';
/** @var string */
protected const SUCCESS_MESSAGE_TYPE = 'success';
/** @var MessageManagerInterface */
protected MessageManagerInterface $messageManager;
public function __construct(
MessageManagerInterface $messageManager
) {
$this->messageManager = $messageManager;
}
public function afterExecute(Action $subject, $result)
{
if ($this->messageManager->getMessages()->getLastAddedMessage()->getIdentifier() === self::ADD_TO_CART_SUCCESS_MESSAGE_IDENTIFIER) {
$this->messageManager->getMessages()->deleteMessageByIdentifier(self::ADD_TO_CART_SUCCESS_MESSAGE_IDENTIFIER);
} else if ($this->messageManager->getMessages()->getLastAddedMessage()->getType() == self::SUCCESS_MESSAGE_TYPE){
$this->messageManager->getMessages()->deleteMessageByIdentifier(self::DEFAULT_MESSAGE_IDENTIFIER);
}
return $result;
}
}
Achieving this is rather easy. Create a basic module under
app/code/<vendor>/<module>with/registration.php
/etc/module.xml
Now the way you can remove the Add to Cart Message is to watch it's observable event and remove it after dispatch. Create
/etc/events.xmlwith following content:So when
checkout_car_add_product_completeis dispatched, the observerAfterAddToCartis called. Create it like this:That's it. Add to Cart Message will not be displayed anymore, while all other messages like Add to Compare etc. will still be displayed.
This solution is not mine originally but I can't remember where I found it.