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
our 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 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:

<?php
require_once ‘app/Mage.php’;
umask(0);
Mage::app(‘default’);
// Add the code you want to execute here:

?>
Integrate-Business-Apps-through-APPSeCONNECT

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;
height:100px;”></textarea>

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):

<?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);
}
}

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()):

<?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();
?>

 

Integrate-Business-Apps-through-APPSeCONNECT

You may also like:
Magento 2.0 Advanced Pricing
Magento’s New Partnership with Adobe
Top ERP Packages to Integrate with Magento