Hi Guys, today we are going to talk about how to register and call an existing event in Magento. If you ask best practice developers then the very first advice they will give it to you is to look for Magento events instead of overwriting Magento classes.
Overwriting Magento classes are costly in terms of maintenance and as you know Magento versions are changing quite frequently now which means you need to check all your overwritten Magento classes to make sure that you are not missing on any security or bug fixes which Magento might have addressed in their newer version. This is the most important reason, we always recommend all the Magento developers to adapt this to the top of their Magento best practices list.
We are going to take checkout_type_onepage_save_order_after event as an example for today’s tutorial. All the events in Magento are registered using Mage::dispatchEvent function which takes the following two parameters -:
- Name of the event (string)
- Data of the event (array)
If you take our example, here is how Magento has registered checkout_type_onepage_save_order_after event in saveOrder function of appcodecoreMageCheckoutModelTypeOnepage.php file.
Mage::dispatchEvent(‘checkout_type_onepage_save_order_after’,
array(‘order’=>$order, ‘quote’=>$this->getQuote()));
In the above example, checkout_type_onepage_save_order_after is a name of the event and $order and $quote objects are the data of the event.
Lets crack on with calling a Magento registered event using our step by step implementation -:
Step 1 – Create a new module under your local folder, in our case under /app/local/Scommerce/Sales.
Step 2 – Register the module in etc/modules directory either in Scommerce_All.xml or Scommerce_Sales.xml by using the following code -:
<Scommerce_Sales> <active>true</active> <codePool>local</codePool> </Scommerce_Sales>
Step 3 – Create config.xml under etc directory of your module applocalScommerceSalesetcconfig.xml by using the following code -:
<?xml version="1.0"?> <config> <modules> <Scommerce_Sales> <version>0.0.1</version> <Scommerce_Sales> </modules> <frontend> <events> <checkout_type_onepage_save_order_after> <observers> <send_order_info_to_third_party> <class>Scommerce_Khaos_Model_Observer</class> <method>sendOrderToThirdParty</method> <send_order_info_to_third_party> </observers> </checkout_type_onepage_save_order_after> </events> </frontend> </config>
Step 4 – Create Observer class in appcodelocalScommerceSalesObserver.php using the following code -:
<?php class Scommerce_Khaos_Model_Observer { public function sendOrderToThirdParty($observer) { try{ $order_id=$observer->getEvent()->getOrder()->getId(); $order = Mage::getModel("sales/order")->load($order_id); $increment_id=$order->getIncrementId(); $this->exportOrder($increment_id); } catch (Exception $e){ Mage::logException($e); } } public function exportOrder($increment_id) { ......... } }
Using the above module you can send order information to third party using API, CSV or XML or any other format to your back office system. Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.
That’s it, it is as simple as that. Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.