Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Magento 2 Development Cookbook

You're reading from   Magento 2 Development Cookbook Over 60 recipes that will tailor and customize your experience with Magento 2

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785882197
Length 304 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Upgrading from Magento 1 FREE CHAPTER 2. Working with Products 3. Theming 4. Creating a Module 5. Databases and Modules 6. Magento Backend 7. Event Handlers and Cronjobs 8. Creating a Shipping Module 9. Creating a Product Slider Widget 10. Performance Optimization 11. Debugging and Unit Testing Index

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 to add an event observer for the helloworld_register_visit event, we have to add the following configuration to the app/code/Packt/HelloWorld/etc/events.xml file with the following content:

    <?xml version="1.0"?>
    <config xmlns:xsi="http:...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime