How to Override Magento Event Observer: A Step-by-Step Guide

Posted on: 04 Nov 2019 by Admin

Here you will learn on how to Override Magento event and observer. Working with Magento events and observer are one of the best ways to extend the core functionality of the Magento Platform. Both Magento 1 and 2 provides the same events and observer customizations.

What is Magento Event?

Events are dispatched by Magento modules whenever specific actions are triggered. As a result of its own events, Magento allows creating your own events that can be dispatched in your code.
When an event gets dispatched, it can pass data to any observer configured to which events it relates to. If an event is to be dispatched you have to call the dispatch function of the event manager class and provide it with the name you want to dispatch along with an array of data you wish to provide.

Dispatching Method

Dispatch Magento Events using the Magento\Framework\Event\Manager Class

What is Magento Observer?

Observers are some type of classes that includes general behavior, performance or change business logic. The Observers get executed whenever the event they are configured to watch is dispatched by the event manager. For creating an observer file, you must clas file under your module /Observer directory. Your Observer class should implement Magento\Framework\Event\ObserverInterface and define its execute function.

How to Override Magento Event and Observer?

As you know Magento comes with many functionalities which helps Magento developers to implement its core files bit easier.
Here we will have to override the Magento observers, when dealing with the extension’s conflicts or may have to disable or modify a part of some extension’s functionality.
Basically, there is no major difference between the observer’s overriding and overriding of any other regular model. In some cases, it’s that not so easy to override.
There might be some sort of issues faced while working with a simple override.
Now Let’s look into the steps which today’s top Magento Development Company use for overriding Magento event browser.
First-of-all pay attention to the naming. Assume that we have some third-party extension which is named as Xyz_Extension, and this extension has the following observer:

class Xyz_Extension_Model_Observer
{
public function xyzMethod( $observer )
{
// Code for an Event written here
return $observer;
}
}

This observer is catching some event using xyzMethod(), and it is described in the config.xml file as shown below :

<config>
<modules>
<xyz_extension>
<version>1.0.0</version>
</xyz_extension>
</modules>
<global>
<models>
<xyz_extension>
<class>Xyz_Extension_Model</class>
</xyz_extension>
</models>
<events>
<event_name>
<observers>
<xyz_extension_observer>
<type>singleton</type>
<class>xyz_extension/observer</class>
<method>xyzMethod</method>
</xyz_extension_observer>
</observers>
</event_name>
</events>
</global>
</config>

Now we should override observer’s method – xyzMethod().
Let’s prepare our extension for this stage:
We should make our extension dependent on the other extension (which is being overridden) in the Egits_Extension.xml. So first register our module in app/etc/modules as below:

<config>
<modules>
<egits_extension>
<active>true</active>
<codepool>local</codepool>
<depends>
<xyz_extension>
</xyz_extension></depends>
</egits_extension>
</modules>
</config>

Then create config.xml file within your module’s etc directory. In this case, we can make a conventional model rewrite. We have to add the rewrite to the model’s node in config.xml:

<config>
<modules>
<egits_extension>
<version>1.0.0</version>
</egits_extension>
</modules>
<global>
<models>
<egits_extension>
<class>Egits_Extension_Model</class>
</egits_extension>
<xyz_extension>
<rewrite>
<observer>Egits_Extension_Model_Rewrite_Observer</observer>
</rewrite>
</xyz_extension>
</models>
</global>
</config>

Here is the rewritten observer file:

class Egits_Extension_Model_Rewrite_Observer extends Xyz_Extension_Model_Observer
{
public function xyzMethod( $observer )
{
// Event's code rewritten here for our purpose
return $observer;
}
}

Now we should adjust the model call to use pseudonyms in the original observer’s identifier by rewriting it. The whole config.xml of our extension will look like this:

<config>
<modules>
<egits_extension>
<version>0.1.0</version>
</egits_extension>
</modules>
<global>
<models>
<egits_extension>
<class>Egits_Extension_Model</class>
</egits_extension>
<xyz_extension>
<rewrite>
<observer>Egits_Extension_Model_Rewrite_Observer</observer>
</rewrite>
</xyz_extension>
</models>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<xyz_extension_observer>
<type>singleton</type>
<class>Xyz_Extension_Model_Observer</class>
<method>xyzMethod</method>
</xyz_extension_observer>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</global>
</config>

This approach allows us to make the conventional rewrite using a safe way. Finally we would prefer to mention that, there was no interference into the Xyz_Extension code.
And that’s it!
We hope that this article will help improve your skills in the programming work.
Thank you for reading our blog!