How to add Order Comment on Customer Checkout in Magento?
This is by far one of the most popular question among the Magento users. Many users mentioned that a Remarks/Comment field was previously available in the Checkout page by default in the older Magento version. Here is an easy way to add the missing Remarks/Comment field in the customer checkout process by means of which the consumers can give their order comments without any trouble.
Add an EAV- Attribute to the Order Entity Model
Create a new local module or take an existing one, in which you place the needed changes/additions.
Create an EAV-Attribute for the order model by executing the php-code as shown below:
$c = array (
‘entity_type_id’ => 11, // 11 is the id of the entity model “sales/order”. This could be different on
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();
This code has to be executed in the proposed Magento environment once, so the eav attribute gets created. One option to do this is to create and run a PHP file like below:
// Add the code you want to execute here:
Add a HTML text area field in one of the checkout templates
Overwrite a checkout template to add the text area field for remarks/comments. We did this in the file app/design/frontend/default/default/template/checkout/onepage/agreements.phtml. Place the new text area 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 System->Configuration at Admin Panel if you want to place the text area in agreements.phtml. You can enable it through Magento Admin Panel: System-> Configuration-> Sales -> Checkout-> Checkout Options-> Enable Terms and Conditions)
<textarea name=”myCustomerOrderComment” id=”myCustomerOrderComment” style=”width:450px;
At Checkout Grab the submitted comment from the request variables
After the customer placed the order, capture the submitted comment from the request variables and call the automatically created setter for the order comment attribute, we 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 an xml-configuration in your local 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);
That’s all you require to save the comment automatically in Magento. Now as a last step 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);