For 2 years of working with Magento, I have met many times when changing a module for my needs, developers often change the module itself (block, model, helper), while not using the possibilities 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 provides great opportunities for developers who implement this system.
To customize magento, you can use the rewriting of methods in blocks and modules (this is the most common method), but you can use another way - the creation of 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 modify the standard logic, and define an event in it with an observer. Creating a module is described in several other articles on habr, so I will not describe the full structure of the module, and I will limit myself 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 redirecting to the designated place, return json data for the ajax module.
Create a new module structure and add a part of the code to the
global part of the
config.xml :
<global> <events> <checkout_cart_add_product_complete> <observers> <ajax_addtocart_complete> <type>singleton</type> <class>ajaxcart/observer</class> <method>addToCartComplete</method> </ajax_addtocart_complete> </observers> </checkout_cart_add_product_complete> </events> </global>
Consider this part in more detail.
<events>
- indicates magento that we will use our own event in our
<type>
- set how the observer model will be called. You can also specify model and object
<checkout_cart_add_product_complete>
- we specify what event to listen
<observers>
- in this block we define our own event
<ajax_addtocart_complete>
- we create our own event (the name is any, but it would be better to call it so that it would be clear that we would not get confused later)
<class>ajaxcart/observer</class>
- the path to the model, where our method specified in the event will lie
<method>addtocartcomplete</method>
- directly the method itself in the class indicated above.
That is, with this part, we indicated that when “checkout_cart_add_product_complete” occurs, the addToCartComplete method should be called with ajaxcart / observer.
Then we create our model in our new module and add the method there.
<?php class My_Model_ObserverModel{ function addToCartComplete($observer){ Mage::getSingleton('checkout/session')->setNoCartRedirect(true); $response['sidebarcart'] = Mage::helper('helperName')->renderSidebarCart(); Mage::app()->getFrontController()->getResponse()->setBody(Mage::helper('core')->jsonEncode($response)); } }
Here we indicate to 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 a json of our javascript function.
To understand how it works, let's take a look at the event that we are listening to “checkout_cart_add_product_complete”.
When the product is added to the card, the public function
addAction () from
Mage_Checkout_CartController is called.If the item has been successfully added, it is executed.
Mage::dispatchEvent('checkout_cart_add_product_complete', array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()) );
What is really happening. At this point, all events are collected for all modules that have been waiting for 'checkout_cart_add_product_complete' and are executed in order of priority.
Also, all the parameters that are passed through Mage :: dispatchEvent also go to your addToCartComplete method. In the current example, the test you receive the product itself, which was added, as well as the Response and Request object. To know exactly what arguments are passed to our event created, you must find the call of the event that we are listening to, and see the additional arguments.
It should be noted that it is not always possible to find a suitable system event in magento. Sometimes it simply does not have the necessary part of logic and then it is precisely necessary to use the block or model redefinition.
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.