Home >Magento Event and Rewrite

Magento Event and Rewrite

Event – Observer system in Magento

When an event gets fired on one side (such as a customer login, load or save in a model, etc.), observers on another side listen for specific events and execute certain logic when specific event is fired.

 – To dispatch an event, we use code

 – To listen to an event, we declare in file NameSpace/YourModule/etc/config.xml

listen-event-magento-1

listen-event-magento-2

In which:

global: Scope – which can also be global, frontend, adminhtml

event_name: Name of the event that you are listening to, which is declared in function Mage::dispatchEvent();

unique_id: This must be unique in all running module on the site

class: Class will be called when event_name is listened

method: This is the function to be called in class

Then run this code when the event is listened

images

In this code, $observer is a variable which is passed in function Mage::dispatchEvent(); (variable_one, variable_two)

Refer some default events by Magento:

 

 

Magento Events Cheat Sheet 1.9

Magento 1.x Events Reference

Magento Overriding Class

Sometimes we need to modify some core functionality of Magento code. However, this action is not encouraged, as it could lead to losses in changes after upgrading Magento to the latest version. Following are two options for changing the core functionality of Magento code.

1. Copy core file to coodpool local by following the instruction

For example, when modify file app/code/core/Mage/Customer/Model/Customer.php, copy file to app/code/local/Mage/Customer/Model/Customer.php and then make changes. At this time, Magento will conduct the code in codepool local file instead of core file

Pros and Cons of this method

Pros:    + This method is fast

              + Changes won’t  be lost after upgrading Magento

Cons:   + You have to copy the whole file while changing only one function

              + Some necessary changes of the code core may not be received when upgrading Magento to the latest version

2. Write Module to Rewrite

a. Rewrite Blocks

 – Declare rewrite in file NameSpace/YourModule/etc/config.xml 

 images

 – Create class as declaring in file config.xml

app/code/local/NameSpace/YourModule/Block/Customer/Account/Forgotpassword.php

 images

b. Rewrite Helpers

Similar to block, we perform in 2 steps

 – Declare rewrite

 images

– Create class app/code/local/NameSpace/YourModule/Helper/Customer/Data.php

 images

c. Rewrite Models

 – Declare rewrite (model and resource model)

 images              

– Create class app/code/local/NameSpace/YourModule/Model/Customer.php 

 images

 images

d. Rewrite Controllers

Rewrite controller is a little different from the classes above

 – Declare in file etc/config.xml:

frontend controller

images

admin controller

images

– Declare class controller needed to rewrite

Frontend controller

images

          Admin controller

images

Pros and Cons of this method

Pros:    + Edit only one function needed to rewrite

              + Restrict maximum number of problems when upgrading Magento to the lastest version

Cons:   + Complicated declaration and config.

< Previous Post
Next Post >
+ posts