Magento Forum

Page 1 of 8
How to add a order comment on cutomer checkout? 
 
mmbl13
Jr. Member
 
Total Posts:  5
Joined:  2008-04-13
 

Hi,

i think i could remember that in earlieer versions of magento i have seen a textfield on the checkout page where customers could add their individual comment. Am i wrong?  Is it possible to get a textfield there?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
hummingbird133
Jr. Member
 
Total Posts:  9
Joined:  2008-05-14
 

I would love to know this also.  Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
sonowiam
Jr. Member
 
Total Posts:  20
Joined:  2008-04-14
 

Me too. Where did it go?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jeena Paradies
Jr. Member
 
Avatar
Total Posts:  22
Joined:  2008-03-10
 

I would like to have one in my shop too.

 
Magento Community Magento Community
Magento Community
Magento Community
 
TanRambun
Sr. Member
 
Avatar
Total Posts:  93
Joined:  2008-11-16
Bangkok, Thailand
 

I implemented a simple, straight forward solution, because i needed this feature too. It not very difficult, most of the work is to customize the templates, on which you like to show the customer comment (e.g. order confirmation email, order details in customer account, order details in the backend, ...).

I will try to explain what i did. There are mainly three things to do:

1.) Add an EAV-Attribute to the order entity model

2.) Add a HTML textarea field in one of the checkout templates

3.) After the customer placed the order, grab the submitted comment from the request variables and call the automatically created setter for the order comment attribute

Here is how i did it in detail:

Create a new local module or take an existing one, in which you place the needed changes/additions.

Create the EAV-Attribute for the order model by executing the php-code below (you can save this code for further use in other projects in the file mysql4-install-0.1.0.php in the sql directory of the module):

$c = array (
  
'entity_type_id'  => 11,         // 11 is the id of the entity model "sales/order". This could be different on your system! Look at database-table "eav_entity_type" for the correct ID!
  
'attribute_code'  => 'myorder_customercomment',
  
'backend_type'    => 'text',     // MySQL-DataType
  
'frontend_input'  => 'textarea'// Type of the HTML-Form-Field
  
'is_global'       => '1',
  
'is_visible'      => '1',
  
'is_required'     => '0',
  
'is_user_defined' => '0',
  
'frontend_label'  => 'Customer Comment',
);
$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id']$c['attribute_code'])
          ->
setStoreId(0)
          ->
addData($c);
$attribute->save();
This code has to be executed in the initialized magento environment once, so the eav attribute gets created. One possibility to do this is to create and run a PHP file like this:
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
// Add the code you want to execute here:

?>

Overwrite a checkout template to add the textarea field. I did this in the file app/design/frontend/default/default/template/checkout/onepage/agreements.phtml. Place the new textarea inside the form (opening and closing form tags) to be sure the variable is submitted. (Keep in mind that you have to “enable Terms and Conditions” in configuration if you want to place the textarea in agreements.phtml. You can enable it here: System > Configuration > Sales > Checkout > Checkout Options > Enable Terms and Conditions)

<textarea name="myCustomerOrderComment" id="myCustomerOrderComment" style="width:450px;height:100px;"></textarea>

Grab the submitted variable. I did this with a helper-class which is called by the event checkout_type_onepage_save_order when placing the order. For this you will need a xml-configuration in your module (app/code/local/MyCompany/MyOrder/etc/config.xml):

<?xml version="1.0"?>
<config>
  <global>
    <
modules>
      <
MyCompany_MyOrder>
        <
version>0.1.0</version>
      </
MyCompany_MyOrder>
    </
modules>

    <
helpers>
      <
myorder>
        <class>
MyCompany_MyOrder_Helper</class>
      </
myorder>
    </
helpers>    
  </global>
  
  <
frontend>
    <
events>
      <
checkout_type_onepage_save_order>
        <
observers>
          <
myorder_set_customerordercomment>
            <
type>model</type>
            <class>
MyCompany_MyOrder_Helper_CustomerOrderComment</class>
            <
method>setCustomerOrderComment</method>
          </
myorder_set_customerordercomment>
        </
observers>
      </
checkout_type_onepage_save_order>
    </
events>
  </
frontend>  
</
config>

And you also need the helper class (app/code/local/MyCompany/MyOrder/Helper/CustomerOrderComment.php):

<?php
class MyCompany_MyOrder_Helper_CustomerOrderComment extends Mage_Core_Helper_Abstract
{
    
public function setCustomerOrderComment($observer)
    
{
        $orderComment 
$this->_getRequest()->getPost('myCustomerOrderComment'false);
        
$observer->getEvent()->getOrder()->setMyorderCustomercomment($orderComment);
    
}
}

Thats all you need to save the comment automatically in the database. Now all you have to do is to customize the templates where you like the comment to be shown (don’t forget to escape the data with something like htmlspecialchars()):

<?php
// If there is an order object:
echo $_order->getMyorderCustomercomment();
// If you only have the order-id (e.g. 11), you have to create an instance first:
$_order Mage::getModel('sales/order')->load(11);
echo 
$_order->getMyorderCustomercomment();
?>

Surely there are better solutions for this problem. If you found another way of implementing order comments for customers, please tell us how you did it.

EDIT
Feb. 19 2009:
- Added instructions for the execution of PHP code (EAV attribute creation).
- Added hint for enabling “Terms and Conditions”.
Mar. 06 2009:
- Added hint about finding the correct ID for entity model “sales/order”.

 
Magento Community Magento Community
Magento Community
Magento Community
 
All About Doors and Windows
Member
 
Avatar
Total Posts:  51
Joined:  2008-10-13
Kansas City, MO USA
 

This works for now! Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Azonti_lee
Jr. Member
 
Total Posts:  2
Joined:  2008-10-13
 

Can someone provide the code in the zip as i couldnt get this to work. i get the event observer to fire when the order is being saved, but when I call :

$orderComment = $this->_getRequest()->getPost(’myCustomerOrderComment’, false);

the posted variable is not being populated.

The textbox i put on the agreements section is:

<textarea id="myCustomerOrderComment" style="width: 450px; height: 100px;” name="myCustomerOrderComment"/>

Can someone tell me if i am doing something stupid?

Many THanks,

Lee

 
Magento Community Magento Community
Magento Community
Magento Community
 
TanRambun
Sr. Member
 
Avatar
Total Posts:  93
Joined:  2008-11-16
Bangkok, Thailand
 
Azonti_lee - 30 January 2009 01:54 PM

Can someone provide the code in the zip as i couldnt get this to work. i get the event observer to fire when the order is being saved, but when I call :

$orderComment = $this->_getRequest()->getPost(’myCustomerOrderComment’, false);

the posted variable is not being populated.

Hi Lee,

did you try something like var_dump($_POST) to see, if the variable is transmitted when placing the order?

Sorry, but there is no more source-code i could provide for this. For me it works like this with the current version of magento (1.2.1). It worked with 1.1.8 before too.

 
Magento Community Magento Community
Magento Community
Magento Community
 
matt5409
Jr. Member
 
Total Posts:  15
Joined:  2008-12-22
 

hi there, i’ve been looking for this for hours. i understand you need to create a new module (which i actually think is ridiculous), but have a few Q’s:

- where to we add these EAV attributes in step 1?
- can creating a module handle multiple fields? i need a textarea and two radio buttons.

thanks in advance
Matt

 
Magento Community Magento Community
Magento Community
Magento Community
 
juppwatis
Sr. Member
 
Total Posts:  113
Joined:  2009-02-01
Germany
 

Is there an extension for the text field at the check out? Would be great, because I am afraid to mess up the code in the files

Kind regards

David

 
Magento Community Magento Community
Magento Community
Magento Community
 
TanRambun
Sr. Member
 
Avatar
Total Posts:  93
Joined:  2008-11-16
Bangkok, Thailand
 
juppwatis - 28 February 2009 02:16 PM

Is there an extension for the text field at the check out? Would be great, because I am afraid to mess up the code in the files

If someone wants to pack this things into a community extension, please feel free to do so. This would be very useful for non-programmers. I am sorry, i don’t have the time for this at the moment.

@juppwatis:
But maybe you just give it a try to do it as local extension. It’s really simple and clean to implement. And with a locale extension you have maximum flexibility.

 
Magento Community Magento Community
Magento Community
Magento Community
 
juppwatis
Sr. Member
 
Total Posts:  113
Joined:  2009-02-01
Germany
 
TanRambun - 01 March 2009 03:12 AM

juppwatis - 28 February 2009 02:16 PM
Is there an extension for the text field at the check out? Would be great, because I am afraid to mess up the code in the files

If someone wants to pack this things into a community extension, please feel free to do so. This would be very useful for non-programmers. I am sorry, i don’t have the time for this at the moment.

@juppwatis:
But maybe you just give it a try to do it as local extension. It’s really simple and clean to implement. And with a locale extension you have maximum flexibility.

What does it mean “local extension”? Can you tell me more about it?

Kind regards

David

 
Magento Community Magento Community
Magento Community
Magento Community
 
TanRambun
Sr. Member
 
Avatar
Total Posts:  93
Joined:  2008-11-16
Bangkok, Thailand
 
juppwatis - 01 March 2009 03:21 AM

What does it mean “local extension”? Can you tell me more about it?

In the folder /app/code/ you will find three subfolders “community”, “core” and “local”. Every modification/extension, which is not done with a community module, goes into the “local” folder. Please try to find some documentation about creating modules, e.g. “php|architect’s Guide to ProgrammingMagento”. You will definitly need this to do a clean customizing of your shop.

 
Magento Community Magento Community
Magento Community
Magento Community
 
stringify
Jr. Member
 
Total Posts:  9
Joined:  2008-06-20
 

It would be really really great if someone would package this as an extension! I don’t have the technical know-how to do it, but this would be very useful to me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Geraint
Jr. Member
 
Total Posts:  5
Joined:  2007-09-15
 

Trying this with 1.2.1.2 I’m having a problem saving the data.

The database entry has been created correctly, setCustomerOrderComment is being called and the value of the attribute is being set correctly but its not being saved in the database. 

When $this->getAttribute($k) is called in Mage_Eav_Model_Entity_Abstract::_collectSaveData it returns a null and thererfore the query that inserts this value in the database doesn’t include this custom field.

I think there’s an extra step required where the attribute is associated with the order before saving - its not enough just to set the value using setMyorderCustomercomment($orderComment). 

Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Geraint
Jr. Member
 
Total Posts:  5
Joined:  2007-09-15
 

I just figured it out - the “entity_type_id” should be 4 not 11 for sales orders smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 8