Remove added to cart message Magento 2

4.2k views Asked by At

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?

3

There are 3 answers

2
Tobi On

Achieving this is rather easy. Create a basic module under app/code/<vendor>/<module> with

/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="0.1.0">
    </module>
</config>

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.xml with following content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_add_product_complete">
        <observer name="your_observer_name" instance="Vendor\Module\Observer\AfterAddToCart" />
    </event>
</config>

So when checkout_car_add_product_complete is dispatched, the observer AfterAddToCart is called. Create it like this:

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Checkout\Model\Cart as CustomerCart;

class AfterAddCart implements ObserverInterface
{

    private $cart;

    public function __construct(
        CustomerCart $cart
    ){
        $this->cart = $cart;
    }

    public function execute(EventObserver $observer)
    {
        $this->cart->getQuote()->setHasError(true);
    }
}

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.

0
WOLFF On
  1. di.xml <preference for="Magento\Checkout\Controller\Cart\Add" type="<Vendor>\<Module>\Controller\Cart\Add"/>
  2. Extend Add Cart controller and in execute()
$this->messageManager->getMessages()->deleteMessageByIdentifier('addCartSuccessMessage');
0
SeStro 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;
    }
}