Tag Archives: Observer

Magento 2 Events

Events are dispatched by Magento 2 modules whenever specific actions are triggered. When an event dispatches, it passes data to the observer that is configured to watch (or monitor) that event. You can dispatch Magento 2 Events using the Magento\Framework\Event\Manager class. This class can be obtained through dependency injection by defining the dependency in the constructor method of the class.

To dispatch an event, you have to call the dispatch function of the event manager class, providing the name of the event along with an array of data that you wish to deliver to observers.

Magento 2 Events

As you have heard, Magento 2 is using the events driven architecture which will help too much to extend the Magento functionality. We can understand this event as a kind of flag that rises when a specific situation happens. .

Dispatch event

In Magento 2 Events List, we can use the class Magento\Framework\Event\Manager to dispatch event. For example, we create a controller action in PHPCodez_First to show the word “Hello World” on the screen: Continue reading Magento 2 Events

Magento Observer Events

Magento uses a fantastic event hooking system, following the Observer Design Pattern, allowing additional functionality to be plugged in and out without modifying the core code.

This list of events is automagically extracted directly from the Magento codebase. It’s pretty unlikely that anything was missed but if you do notice something, get in touch.

Create an observer Magento

Here this observer will display an additional message whenever a product is added to the cart.

Here the name space is PHPCodez

1) Change the directory to the root of magento installation

2) vi app/etc/modules/PHPCodez_Checkout.xml

3) Paste here the below given code

<?xml version=”1.0″?>

<config>

       <modules>

               <PHPCodez_Checkout>

                       <active>true</active>

                       <codePool>local</codePool>

               </PHPCodez_Checkout>

       </modules>

</config>

4) mkdir app/code/local/ (Run this command if local directory is not created )

5) mkdir app/code/local/PHPCodez

6) mkdir app/code/local/PHPCodez/Checkout

7) mkdir app/code/local/PHPCodez/Checkout/etc

8) mkdir app/code/local/PHPCodez/Checkout/Model

9) vi app/code/local/PHPCodez/Checkout/etc/config.xml

10) Paste here the below given code

<?xml version=”1.0″?>

<config>

<modules>

<PHPCodez_Checkout>

<version>0.1.0</version>

</PHPCodez_Checkout>

</modules>

<global>

<events>

<checkout_cart_product_add_after>

<observers>

<PHPCodez_Checkout_Model_Observer>

<type>singleton</type>

<class>PHPCodez_Checkout_Model_Observer</class>

<method>CustomerEdit</method>

</PHPCodez_Checkout_Model_Observer>

</observers>

</checkout_cart_product_add_after>

</events>

</global>

</config>

11) vi app/code/local/PHPCodez/Checkout/Model/Observer.php

12)  Paste here the below given code

<?php

       class PHPCodez_Checkout_Model_Observer {

               public function CustomerEdit($observer) {

                       echo Mage::getSingleton(‘checkout/session’)->addSuccess(“Inside Obserevr”);

               }

       }

?>

13) Clear the cache and check it by adding the product to cart .

Download source file

Hope it would be helpful

Event and Observer in Magento

Magento has been programmed to use Event-Observer methodology and can raise events in crucial areas of the flow.

Observer is the event handler and it allows us to add our own logic in adition to the core logic without modifying the core code .

Event:  An Event is something that occurs in a certain place during a particular sequence flow.

Observer :   An Observer is an event handler. It listens to any event it is attached to and accordingly reacts to the event.