In this Ecommerce era, when everyone is looking for all in one ultimate platform that fulfils all your business needs, Magento is topping a list with its functionality, features & security enhancements that it offers for their users. Not just by features, but you can also integrate third-party extensions one can customize Magento core functionality according to your needs. But, we always suggest you to do not modify core functionality instead you can override code functionality or you can make use of Magento Extensions to make things easy. But have you ever wonder, how it works in the background?
Any Magento 2 Extensions, you have used till date is made up so many of lines of codes that contain different events and observers. Event is nothing but one kind of action that triggers when a specific action is performed and the Observer is designed to catch those actions. When any action is triggered, it will pass data to the relevant observer that is configured for the dispatched event. There are so many Magento events like perform an action after the product adds to cart, after or before customer account create etc. Whenever any of these events is triggered, it is important to display a relevant message in-store frontend to notify the user as there are thousands of events are builtin. So, today we are again back with another blog that will help you to display an event-related message.
Here, we have implemented the observer called “sales_quote_remove_item” and for that, we want to show an error message if the customer tries to remove any product.
First, we need to declare our Event observer by creating “events.xml” file at this path in our extension folder.
app\code\Vendor\Extension\etc\frontend\

After that, we need to create the “RemovecartbeforeObserver.php” in our extension observer folder.
app\code\Vendor\Extension\Observer\

Tadaa! You have successfully thrown an error exception if your customer tries to remove any product from their shopping cart.

You are free to customize this code according to your displaying message for multiple events or observers.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.

Happy Coding!

Click to rate this post!
[Total: 14 Average: 4]