8

In custom extension, I create observer like this.

app\code\Vendor\Extension\etc\frontend\events.xml

<?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="sales_quote_remove_item">
    <observer name="sales_quote_remove_item_handler" instance="Vendor\Extension\Observer\RemovecartbeforeObserver" shared="false" />
  </event>
</config>

And My Observer:

app\code\Vendor\Extension\Observer\RemovecartbeforeObserver.php

use Magento\Framework\Event\ObserverInterface;

class RemovecartbeforeObserver implements ObserverInterface
{
   public function execute(\Magento\Framework\Event\Observer $observer)
   {
       // HERE IS MY CODE
       $message = "THIS IS CUSTOM ERROR MESSAGE";
       throw new \Magento\Framework\Exception\LocalizedException(__($message));
       return;
    }
 }

My Observer function is called, but it not show custom error on the page. Instade of this it show my message in exception.log file like this.

main.CRITICAL: exception 'Magento\Framework\Exception\LocalizedException' with message 'THIS IS CUSTOM ERROR MESSAGE' in E:\xampp\htdocs\myworks\magento213\app\code\Vendor\Extension\Observer\RemovecartbeforeObserver.php:106 

See how it show default message. enter image description here

  • How did your page look like when the observer was executed? – Rendy Eko Prastiyo Feb 7 '17 at 2:46
  • It show default message "We can't remove the item." instade of custom message. I update question check now. – Dhiren Vasoya Feb 7 '17 at 3:07
  • Are you sure the think you want to execute is executed? How to debug: try sending output to browser like Hello Nasty World! and add exit to exit the code execution above the $message = 'blablabla' line. If you see the message, then you can go to the next step. Try it and let me now if the Hello Nasty World! is sent to the browser. – Rendy Eko Prastiyo Feb 7 '17 at 4:37
  • Yes, If I put exit next to it, it show message on screen. If you want then I provided screen-sort. – Dhiren Vasoya Feb 7 '17 at 4:44
  • What message did you see? The Hello nasty World or We can't remove the item! or something else? Please also provide the screenshot. – Rendy Eko Prastiyo Feb 7 '17 at 4:50
11

I search some more things on this and finaly found my solution,

I need to just change the observer code like this.

app\code\Vendor\Extension\Observer\RemovecartbeforeObserver.php

use Magento\Framework\Event\ObserverInterface;

class RemovecartbeforeObserver implements ObserverInterface
{
   protected $messageManager;
   protected $_responseFactory;
   protected $_url;

    public function __construct(\Magento\Framework\Message\ManagerInterface $messageManager,
    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url)
   {
       $this->messageManager = $messageManager;
       $this->_responseFactory = $responseFactory;
       $this->_url = $url;
   }

   public function execute(\Magento\Framework\Event\Observer $observer)
   {
       // HERE IS MY CODE
       $message = "THIS IS CUSTOM ERROR MESSAGE";
       $this->messageManager->addError($message);
       $cartUrl = $this->_url->getUrl('checkout/cart/index');
       $this->_responseFactory->create()->setRedirect($cartUrl)->sendResponse();            
       exit;
    }
 }
| improve this answer | |
  • Not working at my end don't know why :) – SURENDER SINGH Nov 6 '17 at 10:11
  • perfect answer!!! made my day. +1 :) – SagarPPanchal Sep 18 '18 at 8:08
  • Error message not showing if cart is empty.. – jafar pinjar Jul 12 '19 at 12:14
  • @jafarpinjar what you are trying to do? – Dhiren Vasoya Jul 12 '19 at 12:28
  • @DhirenVasoya,sorry its working for me – jafar pinjar Jul 12 '19 at 12:41
4

Update your class to be like this:

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
use Magento\Framework\Controller\Result\RedirectFactory;

class RemovecartbeforeObserver implements ObserverInterface
{
    protected $messageManager;
    protected $redirectFactory;

    public function __construct(
        MessageManagerInterface $messageManager,
        RedirectFactory $redirectFactory
    ) {
        $this->messageManager = $messageManager;
        $this->redirectFactory = $redirectFactory;
    }

    public function execute(Observer $observer)
    {
        $observer->getRequest()->setParam('item', false);

        $message = "THIS IS CUSTOM ERROR MESSAGE";
        $this->messageManager->addError($message);

        return $this->redirectFactory->create()->setPath('*/*/');
    }
}
| improve this answer | |
  • It show message on screen, but still it remove item from the cart. The goal is if observer generate the error then it not remove item from cart and show our custom message on screen. – Dhiren Vasoya Feb 7 '17 at 5:14
  • sales_quote_remove_item is fired after you remove an item. If you want to do a check just before remove an item, you have to observe sales_quote_item_delete_before, not sales_quote_remove_item. Update <event name="sales_quote_remove_item"> of your xml to <event name="sales_quote_item_delete_before"> – Rendy Eko Prastiyo Feb 7 '17 at 5:34
  • still same output. It show message, but remove item from cart. – Dhiren Vasoya Feb 7 '17 at 5:45
  • Have you cleaned & flushed the cache bin/magento cache:clean && bin/magento cache:flush after updating your xml? – Rendy Eko Prastiyo Feb 7 '17 at 5:49
  • yes, I run command – Dhiren Vasoya Feb 7 '17 at 5:50
0

please follow if above solution is not workable:

stop-product-add-t-cart

Don't forget to upvote if you right diverted.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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