Engine23
blogBannerImage
By Engine23

Magento – creating a custom observer

3 Steps to create a custom observer. These instructions are to create an observer when a customer is in the process of checking out, and we are "Observing" the Save shipping piece. This was needed for a project that we are appending to the Gift Message feature for magento. We are adding some text to every order that passes through regardless if the user entered something or not. For this project I have a Website called Bloom Step 1: create the module xml In /app/etc/modules create an xml file called Bloom_Checkout.xml In this new file add the following:

<?xml version="1.0"?>
<config>
<modules>
 <Bloom_Checkout>
 <active>true</active>
 <codePool>local</codePool>
 <version>0.0.1</version>
 </Bloom_Checkout>
</modules>
</config>

Step 2: Create the folder to contain the files config.xml and Observer.php In /app/code/local/Bloom/ create a folder called Checkout Create 2 sub folders inside /app/code/local/Bloom/Checkout 1. etc 2. Model Inside etc create a file called config.xml The file config.xml should read:

<?xml version="1.0"?>
<config>
 <modules>
 <Bloom_Checkout>
 <version>0.0.1</version>
 </Bloom_Checkout>
 </modules>
 <global>
 <models>
 <Bloom_Checkout>
 <class>Bloom_Checkout_Model</class>
 </Bloom_Checkout>
 </models>
 <events>
 <!-- **** This is the event that it will execute on => checkout_type_onepage_save_order_after *** -->
 <!-- checkout_type_onepage_save_order_after-->
 <checkout_controller_onepage_save_shipping_method>
 <observers>
 <bloom_checkout_success_observer>
 <type>singleton</type>
 <class>Bloom_Checkout_Model_Observer</class>
 <method>save_gift_message</method>
 </bloom_checkout_success_observer>
 </observers>
 </checkout_controller_onepage_save_shipping_method>
 <!--/checkout_type_onepage_save_order_after-->
 </events>
 </global>
</config>

In Model create a file called Observer.php This file should contain:

<?php


class Bloom_Checkout_Model_Observer
{

 /*
 * function save_gift_message()
 * This will, take any current gift message, and append to the end our new
 * text to help our warehouse know what is supposed to go into the bloombox
 *
 */
 public function save_gift_message()
 {
 // Get the checkout session
 $session    = Mage::getSingleton('checkout/session');

 // Get the quote
 $quote      = $session->getQuote();

 // Setup our sku list
 $sku_list   = '';

 // Setup our counter
 $i          = 1;

 // Loop through all the items in the order
 foreach ($session->getQuote()->getAllItems() as $item)
 {

 // If the price for this item is 0, its free and is a bloom box item
 if($item->getBaseCalculationPrice() == 0 )
 {
 // Compare the current loop number to the count of the total amount of items,
 // If it is not the same, then add a comma
 
 if( $i < count( $session->getQuote()->getAllItems() ) )
 {
 $comma = ',';
 }
 else
 {
 $comma = '';
 }
 
 // keep appending to our list
 $sku_list .= $item->getSku().$comma;
 }
 // increment the counter
 $i++;
 }

 // Get the quote ID
 $quoteId                = $session->getQuoteId();
 
 // Get the subtotal
 $subtotal               = Mage::helper('bloombox')->getSubtotal();

 // Get the bloom box level
 $bloomBoxLevel          = Mage::helper('bloombox')->getBloomBoxLevel($subtotal);

 // Get the current message
 $original_gift_message  = Mage::app()->getRequest()->getPost('giftmessage');

 // Get the gift message id
 $gift_message_id        = $quote->getGiftMessageId();

 // Take the current message, and append our message to the end
 $message                = $original_gift_message[$quoteId]['message'];
 $message               .= '******************************';
 $message               .= "Enjoy your " . $bloomBoxLevel . " Bloom Box ( " . $sku_list . " )";
 $message               .= '****************************** Women Helping Women Discover What Really Works! Try it. Love it? Review it! ';

 // save our new message
 $giftMessage            = Mage::getModel('giftmessage/message')->load($gift_message_id);

 // Reset the message so that way nothing extra is saved
 $giftMessage->setMessage('')->save();

 // Save the new message
 $giftMessage->setMessage($message)->save();

 }
}

That should be it, and for any question or assisting reach out to our Magento development team.