Hello,
When we delete item from cart there should be message display like "Product A has been removed from cart."
In magento 2 there is no such message display. Anyone have idea about this and how to display this message on cart page after item deleted from cart.
Thanks
Hello,
To display item delete success message after remove item from cart. You can achieve this by creating an observer.
Create below files:
<Vendor>/<Module>/etc/events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="sales_quote_remove_item"> <observer name="cart-item-remove-success" instance="<Vendor>\<Module>\Observer\CartItemRemoveSuccess" /> </event> </config>
<Vendor>/<Module>/Observer/CartItemRemoveSuccess.php
<?php namespace <Vendor>\<Module>\Observer; use Magento\Framework\Event\ObserverInterface; class CartItemRemoveSuccess implements ObserverInterface { /** * @param \Magento\Framework\Message\ManagerInterface $messageManager */ public function __construct( \Magento\Framework\Message\ManagerInterface $messageManager ) { $this->messageManager = $messageManager; } /** * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { $quoteItem = $observer->getQuoteItem(); $quote = $quoteItem->getQuote(); $product = $quoteItem->getProduct(); $name = $product->getName(); if($name){ $message = __('Product '.$name.' has been removed from cart.'); $this->messageManager->addSuccessMessage($message); } } }
Hello @Jignesh Chauhan,
To display a message when an item is removed from the cart in Magento 2, you'll need to customize your theme or create a module to extend the default functionality.
After creating custom module,
Create di.xml file
Create the file app/code/Vendor/Module/etc/frontend/di.xml with the following:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Checkout\Controller\Cart\Delete"> <plugin name="vendor_module_cart_delete" type="Vendor\Module\Plugin\CartDelete" sortOrder="1"/> </type> </config>
Create Plugin Class:
Create the file app/code/Vendor/Module/Plugin/CartDelete.php with the following content:
<?php namespace Vendor\Module\Plugin; use Magento\Framework\Message\ManagerInterface; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Response\RedirectInterface; class CartDelete { /** * @var ManagerInterface */ protected $messageManager; /** * @var RedirectInterface */ protected $redirect; /** * @param ManagerInterface $messageManager * @param RedirectInterface $redirect */ public function __construct( ManagerInterface $messageManager, RedirectInterface $redirect ) { $this->messageManager = $messageManager; $this->redirect = $redirect; } /** * @param Action $subject * @param callable $proceed * @return \Magento\Framework\Controller\Result\Redirect */ public function aroundExecute(Action $subject, callable $proceed) { $result = $proceed(); $this->messageManager->addSuccessMessage(__('Product has been removed from the cart.')); return $result; } }
This approach uses a plugin to add a success message when a product is removed from the cart. The message will be displayed on the cart page or wherever the you are redirected after the item is removed.
If the issue will be resolved, Click Kudos & Accept as a Solution.