Magento – Adding Custom Shipping Comments at Checkout

Home / Blog / Magento – Adding Custom Shipping Comments at Checkout

Sometimes it is nice to be able to allow customers to specify custom shipping comments during the checkout process; however, since the order isn’t actually created until after this process, you have to hook into various events to get this data to the final order entry. This process can be done in the following steps:

1. Add an input on the checkout process to capture the shipping comments. For my case, I added the following input to available.phtml in checkout/onepage/shipping_method directory:

<textarea id=”shippingComment” name=”shippingComment” rows=”3″ cols=”50″></textarea>

2. Create a couple listener methods that can handle two events: checkout_controller_onepage_save_shipping_method and checkout_type_onepage_save_order.

3. Implement these methods as such:

public function setShippingCommentToQuote($observer)
	{
		$event = $observer->getEvent();
		$quote = $event->getQuote();
		$request = $event->getRequest();

		$orderComment = $request->getPost('shippingComment', false);
		$session = Mage::getSingleton('checkout/session');
		$session->setData('shipping_comment', $orderComment);
	}

	public function setShippingCommentToOrder($observer)
	{
		$session = Mage::getSingleton('checkout/session');
		$shippingComment = $session->getData('shipping_comment');
		 
		if($shippingComment){
			$observer->getEvent()->getOrder()->setShippingComment($shippingComment);
		}
	}

The first method handles the checkout_controller_onepage_save_shipping_method event, and basically just takes the input from the request and stores it in the session. The setShippingCommentToOrder() method handles the checkout_type_onepage_save_order event and sets the comment from the session to the order.

Now, you can view these comments from the order information page in the admin section.

Showing 11 comments
  • Mark Guinn

    Thanks for sharing this stuff. I was wondering how you set up the shipping on that site (Motion Savers) so that it displays TBD for shipping. Is that some default setting I’m missing, or a module you’re using? Thanks.

  • tmillhouse

    “To be determined” was added to the template file for that step of the checkout. The text area was added as well to support custom shipping rules from the user:

    <textarea cols="50" rows="3" name="shippingComment" id="shippingComment"></textarea>
    

    Let me know if you’d like more specific instructions, and I can update this article. We’re glad to help.

  • Charlie Evans

    Thanks for the info – just what I was looking for.

    I’ve added some logging to my test module and everything’s working from the session side of things, but I don’t think it’s saving anywhere to the database as I can’t see the comments on the order info page in admin..

    Do I need to add a shipping_comment field to the database (sales_flat_order?) to store this comment?
    And do I need to update my admin templates to display?

    Any help much appreciated!

  • tmillhouse

    Hi Charlie, thanks for reading.

    You are correct. You’ll want to add this column to the database. You can create a sql setup script (let me know if you’d like advise on how to set this up), and you could write something like this:

    $installer->getConnection()->addColumn($installer->getTable(‘sales/order’), ‘shipping_comment’, ‘varchar(255)’);

    After you have that on the database, once you set this value with either:
    $order->setData(‘shipping_comment’, $comment)

    or

    $order->setShippingComment($comment).

    If you need to save this on both the quote and the order, I discovered a way to do this about a month ago. You can add the new column to both the sales/quote and sales/order tables, then in your observer code, you can just set it on the quote. Then, you can define the mapping strategy in your xml so that it will automatically get mapped to your order table once the order is processed. The configuration will look something like:

    <global>
    <fieldsets>
    <sales_convert_quote>
    <shipping_comment><to_order>*</to_order><shipping_comment> …..

    Let me know if you have any questions, we’re glad to help.

  • tmillhouse

    Also, to see this in the admin panel, just identify the admin template that is printing out order data, and then just put $order->getShippingComment() wherever you’d like it to appear. Since the quote and order tables are flat, you can retrieve it using its magic getter method.

  • Charlie Evans

    Fantastic – thanks so much for your help. All working like a charm :)

  • Cedric

    Please excuse my newbiness. Where do i put the 2 functions? In a specific file?

  • tmillhouse

    You’ll want to put the functions inside an Observer, which you configure in your config.xml. Your Observer can be called Observer.php and should extend Varien_Event_Observer. Something like this:

    < ?php
    
    class Company_Module_Model_Observer extends Varien_Event_Observer
    {
    
    

    With the above example, you're project folder heirarchy would look like app/code/local/Company/Module/Model/Observer.php

    Then you'll have to wire the methods in config.xml tag.

    If you have any more questions, I can be more specific.

  • Erwin Martling

    I just want to mention I am new to weblog and really loved you’re web page. Probably I’m likely to bookmark your blog post . You definitely have excellent article content. Regards for sharing your web-site.

  • Scott Greenlaw

    Straight to the point and well written! Why can’t everyone else be like this?

  • Ruiwant

    “For my case, I added the following input to available.phtml in checkout/onepage/shipping_method directory”

    Would you like to tell me where this direcoty is? THX!

Leave a Comment