Magento – Adding Custom Shipping Comments at Checkout

by tmillhouse on July 18, 2010

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.

{ 2 comments… read them below or add one }

Mark Guinn August 26, 2010 at 10:15 am

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 August 26, 2010 at 10:44 am

“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.

Leave a Comment

Previous post:

Next post: