We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How To Create Events In Magento 2
Steps to create events in Magento 2
Create Dispatch event
Directory
app/code/VendorName/ModuleName/Controller/Index/Example.php
Content for Example.php
<?php
namespace VendorName\ModuleName\Controller\Index;
class Example extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$textDisplay = new \Magento\Framework\DataObject(array('text' => 'Welcome To TheOnlineHelper'));
$this->_eventManager->dispatch('vendorname_modulename_display_text', ['toh_text' => $textDisplay]);
echo $textDisplay->getText();
exit;
}
}
Create events.xml
Directory
app/code/VendorName/ModuleName/etc/frontend/events.xml
Content for 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="vendorname_modulename_display_text">
<observer name="toh_display_text" instance="VendorName\ModuleName\Observer\DisplayText" />
</event>
</config>
Create Observer Class
Directory
app/code/VendorName/ModuleName/Observer/DisplayText.php
Content for DisplayText.php
<?php
namespace VendorName\ModuleName\Observer;
class DisplayText implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$displayText = $observer->getData('mp_text');
echo $displayText->getText() . " - Event </br>";
$displayText->setText('Execute event successfully.');
return $this;
}
}
Flush or Clean Cache
To clean cache, run the following command
php bin/magento cache:clean
if you are looking for Magento Developer, visit Magento Development Agency.
Recent post
new admin user
magent 2.4
add new admin user using front-end
add new admin user using command line
admin user role
user role in magneto 2.4
magento 2.4
custom attribute
related products
product page
category
sub-category
how to add simple products
simple products
products in magento2
how to add simple products in magento2
Magento
store url
Full Page Cache
Translate CMS Pages
configurable product
how to add configurable product in magento2
magento2
magento2 products
eCommerce
Product Attribute Set
favicon
Import Products
Rss Feeds
how to configure rss feeds in magento 2
configuration of rss feeds
magento 2 structure
magento directory structure
module structure
magento design structure
theme structure of magento 2
e-commerce
magento2 agency
product
security
magento 2
attribute
CMS
block
admin-user
admin-role
product-attribute
page-cache
customize url
store
base url
CMS pages
basic structure
theme structure
design structure
root folder
migration
upgraded
speed up
improvements
seperate database
magento 1
shopify
new admin
create admin
elasticsearch
catalog search
simpleproduct
css
javascript
merge css
merge javascript
enable css
enable javascript
newsletter
php 8
compatibility
Multiple Stores
remarketing
boost sales
upsell-products
cross-sell products
payment method
check method
money order method
stock
product stock
configuration
Secure your Admin
VAT
tax classes
email logo
shipping policy
create new API
configure free shipping method
delete order in magento 2
features
magento store scope
cdn
cms-page
ux
uxdesign
magento-ux
ui-vs-ux
uidesign
user experience
product-list
uxscope
promote
marketing
SEO
Configure Email
Order Confirmation
redirect buyer
header
footer
layered-navigation
magento 2 store in multiple languages
inbox
message
Cancel order
edit order
magento and microsoft dynamics
integration
magento-theme
magento-layout
layout
magento-commerce
remove order
magento-store
magento-website
switch to Magento
Welcome message
Influence of Marketing
stock message
contact us form
contact email
admin login captcha
single store mode
single store
store view
magento installation
flat rate
shipping method
contact form
recaptcha
marketing plan
CSP
XSS
adding store view
magneto 2
B2B
B2C
D2C
Magento Commerce
Magento Commerce Cloud
guidepost
Small Business
Country Options
Magento Open Source
Product Search
magento-2-tutorial
rewrites
ecommerce agency
schedule design
customer account
magento hosting
hosting
magento configuration
Insert Images
magento developer
sales email
email configuration
Advanced Inventory
Warehouse
copyright
magneto copyright
copyright notice
update copyright
magento email store
store email address
update email address
Locale Option
programmatically
module
magento module
CMS Static Block
controller in magento2
magento controller
Override php file
plugin
plugin in magento2
helper function
magenot 2
call helper in phtml file
Backup Store
Magento Admin Panel
admin menu
model in magento
invoice in magento2
create invoice
Dependency Injection
di.xml
cronjob
cronjob in magento2
Api
events
event in magento2
Change
Default Icon
Menu Items
Backend
widget in magento 2
widget
custom widget
Add Store Demo
Api in magento 2
Run Compile Commad
custom tab
Mass Actions in magento 2
override classes
preference
helper in magento2
featured products
custom helper
checkout in magento 2
checkout page
customer collection
product collection
wishlist products collection
order collection
review in magento 2
rating in magento 2
review collection
rating collection
recent product collection
ui form
form in magento 2
magento 2 configuration
magento 2 fields
delete action in magento 2
edit action in magento 2
disable payment method
custom discount
virtual type
log
custom log
log file
eav attribute
installschema
database
product schema
production mode
indexer
reindexer
reindex magento 2
indexer magento 2
magento2 installation
email customization
custom email
email in magento 2
indexing
cart rules
catalog price rule
magento 2 catalog
currency in magento 2
command
console command
command in magento 2
store information in magento 2
extend layout
homepage
url in magento 2
error
google shopping feed
feed
magento2 feed
reindex magento 2 programatically
custom indexer
custom indexer for magneto 2
Magento 2 UX
Benefits of Magento 2 UX
magento 2 edit order
cancle order in magento 2
remove order in magento 2
update order in magento 2
magento 2 free shipping rule