Home > Magento > Adding Customer Comments as Order Status Comments using Magento & OneStepCheckout

Adding Customer Comments as Order Status Comments using Magento & OneStepCheckout

October 24th, 2010

A few days ago I got an email from someone asking me about adding the default customer comments in OneStepCheckout (www.onestepcheckout.com) as regular order comments. I thought I’d share this quick & simple hack with everyone, so here’s what you have to do:
Open app/code/local/Idev/OneStepCheckout/Helper/Data.php, and after

$observer->getEvent()->getOrder()->setOnestepcheckoutCustomercomment($orderComment);

add this line:
$observer->getEvent()->getOrder()->setState( Mage_Sales_Model_Order::STATE_NEW, true, $orderComment, false );

$observer->getEvent()->getOrder()->setState( 
    $observer->getEvent()->getOrder()->getStatus(), 
    true, 
    $orderComment, 
    false 
);

This will add the comments on the regular comments/statuses thread, as well as a customer comment. If you want to disable customer comments, just comment the original code.

Actually, this approach should also work with the out-of-the-box Magento one step checkout. Add an input on the last step, create a listener for

checkout_type_onepage_save_order

and use the same piece of code:

$observer->getEvent()->getOrder()->setState( $observer->getEvent()->getOrder()->getStatus(), true, $this->_getRequest()->getPost('order_comments'), false );

Note: Tested on Magento 1.4.1.0 & 1.4.1.1

  1. October 25th, 2010 at 09:33 | #1

    Hi

    This approach has it’s flaws especially in 1.3.* series of Magento where order comments can’t be added without changing the actual order status (as we see you set a “new” status here) so be careful when using something like this as your payment method usually sets a status to order and you don’t need the small parts dealing with order process that customer reps has to follow. Triggering order status can fire up additional actions. If you are sure that this is not bothering you this is actually a good way to implement it.

    OSC adds a new variable to order in 1.3 series and a new database column for 1.4 with flat order data.

  2. Cristi
    October 25th, 2010 at 12:40 | #2

    Thanks for the feedback Anton. Indeed, this has only been tested on 1.4.1.x. Initially, I used $oder->getStatus() and passed that to setState(), then I thought I’d add a status. But you’re right, this could cause problems, so I’ve slightly changed the article and also added a note for compatibility.

  3. Anton
    October 25th, 2010 at 14:13 | #3

    You should add a check if status is set or not cause you are making this in order creation time where the status may or may not exist jet.

    And of-course you need to test test test cause as I said lot of 3d party modules may listen the status event and send e-mails and stuff. That’s the case this is implemented as a a separate attribute or field in OSC in a first place: to avoid collisions or mishaps that are hard to foresee.

    In OSC 3.* the enabling of order comments does not need any template modifications any more so I would leave it as it is.

  4. Cristi
    October 25th, 2010 at 14:16 | #4

    Well, if the status is not yet set then it shouldn’t trigger anything at all – in theory.
    I agree with testing & leaving it as is, especially for OSCv3, but sometimes one may need to add those comments in the order status comments – for example, when using 3rd party software to handle orders.

  5. Anton
    October 25th, 2010 at 15:39 | #5

    sure , i’ll write a wiki article based on your blog post.

  6. October 28th, 2010 at 19:39 | #6

    Thanks 4 this solution, I’ve had allot of problems getting the comment thing to work properly.

  7. Pratik
    November 19th, 2010 at 11:46 | #7

    Yes, it works great, many thanks. keep posting..

  8. Scott
    April 6th, 2011 at 22:32 | #8

    Does anyone know how to mark the original checkout comment as visible on frontend changing the following false to true only sets it to send an email. Are there any other arguments that can be configure for visibility?
    true, $orderComment, false

  1. No trackbacks yet.