• Services

    IT SERVICES

    solutions for almost every porblems

    Ecommerce Development

    Enterprise Solutions

    Web Development

    Mobile App Development

    Digital Marketing Services

    Quick Links

    To Our Popular Services
  • Hire Developers

    Hire Developers

    OUR ExEPRTISE, YOUR CONTROL

    Hire Mangeto Developers

    Hire Java Developers

    Hire Node Developers

    Hire Shopify Developers

    Hire Android Developers

    Hire Angular Developers

    Hire Shopware Developers

    Hire iOS App Developers

    Hire WordPress Developers

    Hire A full Stack Developer

    Choose a truly all-round developer who is expert in all the stack you require.

  • Case Studies
  • About
  • Blog
  • Contact Us
How to Dispatch Custom Events in Magento 2
Updated on 26/08/2024

How to Dispatch Custom Events in Magento 2

ecommerce Magento Technical

In this blog, we’ll explore how to dispatch custom events in Magento, expanding on the concepts of events and observers that we discussed in our previous tutorial. If you’re familiar with observing predefined Magento events, you’ll find it quite straightforward to dispatch your own custom events. Let’s dive in.

Full Video Walkthrough by CodeWithAli

Recap: Observing Predefined Magento Events

In our last tutorial, we covered how to observe predefined Magento events using the Observer class. Observers allow us to react to specific events that occur within Magento, like customer login, order placement, etc. Now, we’ll take it a step further by learning how to fire a custom event from our own code.

read previous blogs of the series

Introduction of Events and Observers

Dispatching a Custom Event

Magento provides a simple way to dispatch custom events through the Manager Interface “Magento\Framework\Event\ManagerInterface”. This interface allows us to trigger events programmatically, which can then be observed by our custom observers.

Step 1: Set Up the Event Manager Interface

First, we’ll need to inject the “EventManager” interface into our class using dependency injection. This will enable us to dispatch events from within our methods.

Go to the LoginAfter Observer we created in the last tutorial “Observer/LoginAfter.php” and make the following changes

Click here to view and edit & add your code between the textarea tags

Step 2: Dispatch the Event

To dispatch an event, we use the dispatch method of the EventManager class. This method requires at least one parameter: the event name. You can also pass additional data as an associative array.

Note: The additional information has to be in an array

Let’s apply this to our example. We’ll dispatch an event named observer_login_after when a customer logs in. Here’s how the code will look like:

Click here to view and edit & add your code between the textarea tags

Checkout Our Magento 2 Extensions

Creating an Observer for Our Event

Now that we’ve dispatched the event, the next step is to observe it.

Step 1: Configure the Event in events.xml

To observe the custom event, you need to configure it in the events.xml file “etc\frontend\events.xml” of your module. This tells Magento which observer to trigger when the event occurs.

Click here to view and edit & add your code between the textarea tags

In the above code, custom_observer_login_after is the unique name of our observer, and Demo\EventObservers\Observer\EventObserversLoginAfter is the class that will handle the event.

Step 2: Create the Observer Class

Next, we’ll create the observer class that will handle our custom event inside the Observer folder and name it EventObserversLoginAfter.php

Click here to view and edit & add your code between the textarea tags

Testing the Observer

Finally, to see everything in action, clear the cache, log out, and then log in again. Check your logs, and you should see an entry for the custom event with the customer’s email and ID.

In this tutorial, we’ve successfully covered how to dispatch a custom event in Magento and how to observe it with a custom observer. Understanding these concepts will give you more control over your Magento application, allowing you to create more dynamic and responsive features.

Stay tuned for more tutorials on Magento development!

Would you like to share this article?

Share

need assisting with Magento 2 customization?

Feel Free To Contact Our Experts

All Categories

Artificial Intelligence
ChatGPT
Communication
ecommerce
Magento
News and Updates
Technical
UX and Navigation
WhatsApp API

Latest Post

  • Magento 2 Rich Text Schema Setup in JSON-LD: Step-by-Step Guide
  • Fixing Magento 2 Product Schema Markup | Missing “image” field FIX
  • How to Dispatch Custom Events in Magento 2
  • Adding Filters in Magento Admin Grid | Column and Non-Column Component
  • How to add custom styles (Colors and Fonts) to Magento Admin Grid

Related Post

  • Magento 2 Rich Text Schema Setup in JSON-LD: Step-by-Step Guide
  • Fixing Magento 2 Product Schema Markup | Missing “image” field FIX
  • Adding Filters in Magento Admin Grid | Column and Non-Column Component
  • How to add custom styles (Colors and Fonts) to Magento Admin Grid
  • Introduction to Events and Observers in Magento 2

310 Kuber Avenue, Near Gurudwara Cross Road, Jamnagar – 361008

Plot No 36, Galaxy Park – II, Morkanda Road,
Jamnagar – 361001

Quick Links

  • About
  • Career
  • Case Studies
  • Blog
  • Contact Us
  • Privacy Policy
Icon-facebook Linkedin Google Clutch Logo White

Our Expertise

  • eCommerce Development
  • Web Development Service
  • Enterprise Solutions
  • Mobile App Development
  • Digital Marketing Services

Hire Dedicated Developers

  • Hire Full Stack Developers
  • Hire Certified Magento Developers
  • Hire Top Java Developers
  • Hire Node.JS Developers
  • Hire Angular Developers
  • Hire Android Developers
  • Hire iOS Developers
  • Hire Shopify Developers
  • Hire WordPress Developer
  • Hire Shopware Developers

Copyright @Azguards Technolabs 2025 all Rights Reserved.