skip to Main Content

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

Answers


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

    /registration.php

    <?php
    MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::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="VendorModuleObserverAfterAddToCart" />
        </event>
    </config>
    

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

    <?php
    namespace VendorModuleObserver;
    
    use MagentoFrameworkEventObserverInterface;
    use MagentoFrameworkEventObserver as EventObserver;
    use MagentoCheckoutModelCart 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.

    Login or Signup to reply.
    1. di.xml <preference for="MagentoCheckoutControllerCartAdd" type="<Vendor><Module>ControllerCartAdd"/>
    2. Extend Add Cart controller and in execute()
    $this->messageManager->getMessages()->deleteMessageByIdentifier('addCartSuccessMessage');
    
    Login or Signup to reply.
  2. You have to add afterExecute plugin to MagentoCheckoutControllerCartAdd 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;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search