O'Reilly logo

Magento 2 Development Cookbook by Bart Delvaux

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding an event observer

If you read the previous recipe, you know how to fire an event using the dispatch() function. In this recipe, you will learn how to execute some code when an event is dispatched and what we can do with it.

Getting ready

In this recipe, we will add two event observers. The first one will catch the event that we created in the previous recipe. The second event listener (observer) will hook into the add to cart action of a product.

In this recipe, we will work further on the Packt_HelloWorld module from the previous recipes. Ensure that you have the right code files installed for this recipe.

How to do it...

In this tutorial, you will discover how to listen for an event and execute your code by performing these steps:

  1. When we want ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required