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"?>

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"?>
 <!-- **** This is the event that it will execute on => checkout_type_onepage_save_order_after *** -->
 <!-- checkout_type_onepage_save_order_after-->

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


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 = ',';
 $comma = '';
 // keep appending to our list
 $sku_list .= $item->getSku().$comma;
 // increment the counter

 // 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

 // Save the new message


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