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):
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:
$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();
// 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):
And you also need the helper class (app/code/local/MyCompany/MyOrder/Helper/CustomerOrderComment.php):
class MyCompany_MyOrder_Helper_CustomerOrderComment extends Mage_Core_Helper_Abstract
public function setCustomerOrderComment($observer)
$orderComment = $this->_getRequest()->getPost('myCustomerOrderComment', false);
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()):
// If there is an order object:
// If you only have the order-id (e.g. 11), you have to create an instance first:
$_order = Mage::getModel('sales/order')->load(11);
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.
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”.