
Customizing Magento Using Event / Observer
Over 2 years of working with Magento, I have met many times when changing a module for their needs, developers often change the module itself (block, model, helper), without using the capabilities provided by Magento itself. Magento is a product that is very convenient for third-party developers, in terms of additions and changes to the basic logic, and gives great opportunities to developers who implement this system.
To customize magento, you can use the rewriting of methods in blocks and modules (this is the most common way), but you can use another way - this is creating an observer and a new event.
To begin with, if you decide to use the monitoring mechanism, you will need to disable compilation. Next, it is best to create a new module in which you will supplement or change the standard logic, and define an event with an observer in it. Creating a module is described in several other articles on Habr, so I will not describe the complete structure of the module, but I will limit myself only to the most necessary.
For example:
after successfully adding a product to the card, we want to change the standard behavior of the checkout module. Instead of forwarding to the designated place, return json data for the ajax module.
We create a new module structure and add such a piece of code to config.xml in the global part:
Let's consider this part in more detail.
That is, with this part, we indicated that when “checkout_cart_add_product_complete” occurs, you will need to call the addToCartComplete method with ajaxcart / observer.
Next, we create our model in our new module and add the method there.
Here we tell the checkout module that we do not need to redirect. Also, for example, I added a call to a custom function that renders the Mage_Checkout_Block_Cart_Sidebar block in html, and then return this html as json to our javascript function.
To understand a little how this works, let's look at the event we are listening to “checkout_cart_add_product_complete”.
When the product is added to the card, the public function addAction () is called from Mage_Checkout_CartController.
If the item is successfully added, it is executed
What is really going on. At this moment, all events are collected for all modules that were waiting for 'checkout_cart_add_product_complete' and are executed in order.
Also, all parameters that are passed through Mage :: dispatchEvent fall into your addToCartComplete method. In the current example, you get the father-in-law of the product itself, which was added, as well as the Response and Request object. In order to know exactly what arguments are passed to our created event, you need to find the call of the event that we are listening to and see additional arguments.
It should be noted that it is not always possible to find a suitable system event in magento. Sometimes it is simply missing the necessary part of the logic, and then you definitely have to use the redefinition of the block or model.
The example is quite simple and primitive - but knowing how events work, you can use them for the benefit of yourself and other developers who will come to your place.
To customize magento, you can use the rewriting of methods in blocks and modules (this is the most common way), but you can use another way - this is creating an observer and a new event.
To begin with, if you decide to use the monitoring mechanism, you will need to disable compilation. Next, it is best to create a new module in which you will supplement or change the standard logic, and define an event with an observer in it. Creating a module is described in several other articles on Habr, so I will not describe the complete structure of the module, but I will limit myself only to the most necessary.
For example:
after successfully adding a product to the card, we want to change the standard behavior of the checkout module. Instead of forwarding to the designated place, return json data for the ajax module.
We create a new module structure and add such a piece of code to config.xml in the global part:
singleton ajaxcart/observer addToCartComplete
Let's consider this part in more detail.
- indicates magento that we will use our own event in our
- set how the observer model will be called. You can also specify model and object
- indicate which event to listen to
- in this block we define our own event
- create our own event (any name but better to be named so that it would be clear that we ourselves would not get confused later).ajaxcart/observer
- the path to the model where our method set in the event will lie addtocartcomplete
- directly the method itself in the class, which is indicated above. That is, with this part, we indicated that when “checkout_cart_add_product_complete” occurs, you will need to call the addToCartComplete method with ajaxcart / observer.
Next, we create our model in our new module and add the method there.
setNoCartRedirect(true);
$response['sidebarcart'] = Mage::helper(‘helperName’)->renderSidebarCart();
Mage::app()->getFrontController()->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}
}
Here we tell the checkout module that we do not need to redirect. Also, for example, I added a call to a custom function that renders the Mage_Checkout_Block_Cart_Sidebar block in html, and then return this html as json to our javascript function.
To understand a little how this works, let's look at the event we are listening to “checkout_cart_add_product_complete”.
When the product is added to the card, the public function addAction () is called from Mage_Checkout_CartController.
If the item is successfully added, it is executed
Mage::dispatchEvent('checkout_cart_add_product_complete',
array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
What is really going on. At this moment, all events are collected for all modules that were waiting for 'checkout_cart_add_product_complete' and are executed in order.
Also, all parameters that are passed through Mage :: dispatchEvent fall into your addToCartComplete method. In the current example, you get the father-in-law of the product itself, which was added, as well as the Response and Request object. In order to know exactly what arguments are passed to our created event, you need to find the call of the event that we are listening to and see additional arguments.
It should be noted that it is not always possible to find a suitable system event in magento. Sometimes it is simply missing the necessary part of the logic, and then you definitely have to use the redefinition of the block or model.
The example is quite simple and primitive - but knowing how events work, you can use them for the benefit of yourself and other developers who will come to your place.