How to override Magento event observer

How to override Magento event 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.

How to override Magento event observer

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 :


    
        
            1.0.0
        
    
    
        
            
                Xyz_Extension_Model
            
        
        
            
                
                    
                        singleton
                        xyz_extension/observer
                        xyzMethod
                    
                
            
        
    

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:


    
        
            true
            local
            
                
            
        
    

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:


    
        
            1.0.0
        
    
    
        
            
                Egits_Extension_Model
            
     
                
                    Egits_Extension_Model_Rewrite_Observer
                     
           
        
    


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:


    
        
            0.1.0
        
    
    
        
            
                Egits_Extension_Model
            
            
                
                    Egits_Extension_Model_Rewrite_Observer
                
            
        
        
            
                
                    
                        singleton
                        Xyz_Extension_Model_Observer
                        xyzMethod
                    
                
            
        
    


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.

We hope that this article will help improve your skills in the programming work.

Thank you for reading our blog!

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn

leave a comment