Eglobe IT Solutions
open

How to override Magento event observer

  • How to override Magento event observer

    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. But sometimes we may face with issues while working with simple override.

    Let’s see the following step by step instructions for Magento observer’s overriding.

    We should pay attention to the naming.

    Assume that we have some third party extension. It is named 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!

  • Post Comments

    Submit Comment




    * Required Fields