How to call or register an events in magento?

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 \app\code\core\Mage\Checkout\Model\Type\Onepage.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 \app\local\Scommerce\Sales\etc\config.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 app\code\local\Scommerce\Sales\Observer.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.

Scommerce

8 Comments

  1. Hey would you mind letting me know which webhost you’re working with? I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot faster then most. Can you recommend a good web hosting provider at a reasonable price? Kudos, I appreciate it!

  2. Spot on with this write-up, I truly believe that this amazing site needs far more attention. I’ll probably be returning to read through more, thanks for the advice!

  3. I will right away take hold of your rss feed as I can’t in finding your email subscription hyperlink or e-newsletter service. Do you’ve any? Kindly let me recognize so that I may just subscribe. Thanks.

  4. Hello everyone, it’s my first visit at this website, and post is actually fruitful in support of me, keep up posting these articles.

  5. Thanks guys for your words of appreciations. We are going to keep on writing more good articles to help the community.

    Keep coming and leave your comments, it really helps us!!!!

  6. Quality articles is the main to interest the visitors to pay a quick visit
    the website, that’s what this web site is providing.

  7. Thanks a bunch for sharing this with all folks you really know what you are talking about!
    Bookmarked.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.